Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Generate a Secure URL to Download File from s3 Using Ruby aws/s3 Gem

I am writing a small script to locate a specific file in a bucket on aws and create a temporarily authenticated url to send to colleagues. (Ideally, this would create a result similar to right-clicking a file in a bucket on the console and copying the link address).

I have looked into paperclip, which doesn't appear to meet this criteria, however I could just not be aware of its full capabilities.

I tried the following:

def authenticated_url(file_name, bucket)
  AWS::S3::S3Object.url_for(file_name, bucket, :secure => true, :expires => 20*60)
end

Which produced this type of result:

...-1.amazonaws.com/file_path/file.zip.AWSAccessKeyId={key}Expires=1200&Signature={...}

Is there a way to create a secure url more similar to the scenario described above that could simply be sent as a link? If not, any secure alternatives would be welcomed.

like image 882
LFoos24 Avatar asked Apr 10 '13 19:04

LFoos24


People also ask

How do I create a URL for Amazon S3?

Nothing could be easier, just select all files you want to generate URLs for and click the “Web URL” button on the toolbar. You will see the list of URLs for each of the selected files. Click copy to clipboard button and you are ready to paste URLs to other programs such as an HTML files editor.

How can you securely upload or download your data to from the S3 service?

You can securely upload/download your data to Amazon S3 via SSL endpoints using the HTTPS protocol. If you need extra security you can use the Server-Side Encryption (SSE) option to encrypt data stored at rest.

In which ways can you access S3 files securely?

Use encryption to protect your data If your use case requires encryption during transmission, Amazon S3 supports the HTTPS protocol, which encrypts data in transit to and from Amazon S3. All AWS SDKs and AWS tools use HTTPS by default.


1 Answers

What you need a called a "Tokenized Link". Fortunately, it's built into the aws-sdk gem you are using.

Here's a previous question that a solution you can use:

How to store data in S3 and allow user access in a secure way with rails API / iOS client?

However, that is a Rails solution which has the fancy Rails time helpers like 20.minutes.from_now. You can either set the expiry date to a specific date by adding a specific number of seconds to the current time like Time.now.to_i + (20 * 60), or include the ActiveSupport time helpers into your ruby script with require 'active_support/core_ext/numeric/time'. That will allow the 20.minutes.from_now stuff to work.

Also, you will need to require the entire aws-sdk gem, not just the S3 part.

like image 161
Nick Messick Avatar answered Sep 25 '22 18:09

Nick Messick