Best way to download a file from S3 on Shrine 3.x

Hello,

This was what I did to download a file from S3 on Shrine 2.x:
redirect_to @image.file(:high).url(download: true)

With Shrine 3.x, there is no issue, but the file is displayed in browser, not downloaded.
What did I miss ?

The :download URL option has been deprecated in 2.18.0 (and removed in 3.0.0) in favour of using the :response_content_disposition option directly.

@image.file(high).url(response_content_disposition: "attachment")

Hum, it’s not working here, or maybe I need to use another thing rather than “redirect_to” ?

redirect_to @image.file(:high).url(response_content_disposition: "attachment")

Actually, the file is always displayed in the browser.
Note that I can write anything in url(), there is no issue.

You might have public uploads enabled. Public (unsigned) URLs don’t support force download links.

Yes, it is. I changed to “public” yesterday.
Is there another way to download the file ?

With Paperclip, I could do this, even on a public S3 file:
redirect_to @model.image.url(:high)

You have two options:

  1. Generate a signed expiring URL for that particular redirect by passing public: false:

    redirect_to @image.file(:high).url(
      public: false,
      response_content_disposition: "attachment",
    )
    
  2. Specify :content_disposition when uploading the file, e.g. you can do it with the upload_options plugin:

    Shrine.plugin :upload_options, store: { content_disposition: "attachment" }
    

Thanks for this tip, it works now !

I can’t use this one cos’ the download is required for some app features (sync from devices for example), but the display of image is required on the webapp by default. Else, I need to force using “inline” in the webapp.
In all cases, I don’t think there is a delay cost when querying images as “attachment” or “inline” ?

In all cases, I don’t think there is a delay cost when querying images as “attachment” or “inline” ?

Yes, there is no additional delay there, the attachment vs inline is just the value of the Content-Disposition response header, nothing else changes. All browsers should start the download as soon as they receive the response headers.

1 Like