Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I actually just implemented authorized S3 url's in my <strong>Ruby on Rails 3</strong> application with <strong>Paperclip</strong>. Let me share how I accomplished this.</p> <p>So what I did, and what you probably want is quite easy to implement. Let me give you an example:</p> <p><strong>FileObject</strong> model</p> <pre><code>has_attached_file :attachment, :path =&gt; "files/:id/:basename.:extension", :storage =&gt; :s3, :s3_permissions =&gt; :private, :s3_credentials =&gt; File.join(Rails.root, 'config', 's3.yml') </code></pre> <p><strong>FileObjectsController</strong> controller</p> <pre><code> def download @file_object = FileObject.find(params[:id]) redirect_to(@file_object.attachment.expiring_url(10)) end </code></pre> <p>I believe this is quite straightforward. You add the Paperclip attachment to the <strong>FileObject</strong> model and then have an action (<strong>download</strong> for example) in the <strong>FileObjectsController</strong>. This way you can do some application level authorization from within your controller with a <strong>before_filter</strong> or something.</p> <p>The <strong>expiring_url()</strong> method (provided by <strong>Paperclip</strong>) on the <strong>@file_object.attachment</strong> basically requests Amazon S3 for a key which makes the file accessible with that particular key. The first argument of the <strong>expiring_url()</strong> method takes an integer which represents the amount of <strong>seconds</strong> in which you want the provided URL to expire.</p> <p>In my application it is currently set to <strong>10</strong> (<strong>@file_object.attachment.expiring_url(10)</strong>) so when the user requests a file, the user ALWAYS has to go through my application at for example <strong>myapp.com/file_objects/3/download</strong> to get a new valid URL from Amazon, which the user then instantly will be using to download the file since we're using the <strong>redirect_to</strong> method in the <strong>download</strong> action. So basically 10 seconds after the user hits the <strong>download</strong> action, the link already expired and the user has (or is still) happily downloading the file, while it remains protected from any non-authorized users.</p> <p>I have even tried to set <strong>expiring_url(1)</strong> so that the URL instantly expires after the user triggers the Amazon S3 request for the URL. This worked for me locally, but never used it in production, you can try that too. However, I set it to 10 seconds to give the server a short period of time to respond. Works great so far and I doubt anyone will hijack someone's URL within 10 seconds after it's been created, let alone know what the URL is.</p> <p>Extra security measure I took is just to generate a <strong>secret key</strong> for every file on create so my URL's always look like this:</p> <pre><code>has_attached_file :attachment, :path =&gt; "files/:id/:secret_key/:basename.:extension" </code></pre> <p>So that every URL has it's unique <strong>secret_key</strong> in it's path, making it harder to hijack within the time the URL is accessible. Mind you that, while the URL to your file remains the same, the accessibility comes from the additional parameters that Amazon S3 provides which expire: </p> <pre><code>http://s3.amazonaws.com/mybucket/files/f5039a57acc187b36c2d/my_file.pdf?AWSAccessKeyId=AKIAIPPJ2IPWN5U3O1OA&amp;Expires=1288526454&amp;Signature=5i4%2B99rUwhpP2SbNsJKhT/nSzsQ%3D </code></pre> <p>Notice this part, which is the key Amazon generates and expires which makes the file temporarily accessible:</p> <pre><code>my_file.pdf?AWSAccessKeyId=AKIAIPPJ2IPWN5U3O1OA&amp;Expires=1288526454&amp;Signature=5i4%2B99rUwhpP2SbNsJKhT/nSzsQ%3D </code></pre> <p>That's what it's all about. And this changes with every request for your file if requested through the <strong>download</strong> action.</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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