Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Akshully, you can: </p> <pre><code># minimal example, # here using mongoid, but it doesn't really matter class Media field :filename, type: String # i.e. "cute-puppy" field :extension, type: String # i.e. "mp4" mount_uploader :media, MediaUploader end class MediaUploader &lt; CarrierWave::Uploader::Base # Files on S3 are only accessible via signed URLS: @fog_public = false # Signed URLS expire after ...: @fog_authenticated_url_expiration = 2.hours # in seconds from now, (default is 10.minutes) # MIME-Type and filename that the user will see: def fog_attributes { "Content-Disposition" =&gt; "attachment; filename*=UTF-8''#{model.filename}", "Content-Type" =&gt; MIME::Types.type_for(model.extension).first.content_type } end # ... end </code></pre> <p>The url that <code>model.media.url</code> yields will then return the following headers: </p> <pre><code>Accept-Ranges:bytes Content-Disposition:attachment; filename*=UTF-8''yourfilename.mp4 Content-Length:3926746 Content-Type:video/mpeg Date:Thu, 28 Feb 2013 10:09:14 GMT Last-Modified:Thu, 28 Feb 2013 09:53:50 GMT Server:AmazonS3 ... </code></pre> <p>The browser will then force a download (instead of opening in browser) and use the filename you set, regardless of the file name use used to store stuff in the bucket. The only drawback of this is that the Content-Disposition header is set when Carrierwave creates the file, so you couldn't for example use different filenames on the same file for different users. </p> <p>In that case you could use RightAWS to generate the signed URL: </p> <pre><code>class Media def to_url s3_key = "" # the 'path' to the file in the S3 bucket request_header = {} response_header = { "response-content-disposition" =&gt; "attachment; filename*=UTF-8''#{filename_with_extension}", "response-content-type" =&gt; MIME::Types.type_for(extension).first.content_type } RightAws::S3Generator.new( Settings.aws.key, Settings.aws.secret, :port =&gt; 80, :protocol =&gt; 'http'). bucket(Settings.aws.bucket). get(s3_key, 2.hours, request_header, response_header) end end </code></pre> <p>EDIT: It is not necessary to use RightAWS, <code>uploader#url</code> supports overriding response headers, the syntax is just a bit confusing (as is everything with CarrierWave, imho, but it's still awesome):</p> <pre><code>Media.last.media.url(query: {"response-content-disposition" =&gt; "attachment; filename*=UTF-8''huhuhuhuhu"}) # results in: # =&gt; https://yourbucket.s3.amazonaws.com/media/512f292be75ab5a46f000001/yourfile.mp4?response-content-disposition=attachment%3B%20filename%2A%3DUTF-8%27%27huhuhuhuhu&amp;AWSAccessKeyId=key&amp;Signature=signature%3D&amp;Expires=1362055338 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload