How to create a secure URL to download a file from s3 Using Ruby aws / s3 Gem

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

I looked at a paper clip that did not seem to meet these criteria, but I just could not know about 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 

What caused this result:

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

Is there a way to create a secure URL, similar to the script described above, which can simply be sent as a link? If not, any safe alternatives would be welcome.

+4
source share
1 answer

What you need is called "Tokenized Link". Fortunately, it is built into the aws-sdk gem that you use.

Here is the previous question, what is the solution you can use:

How to save data on S3 and securely allow user access using the API / iOS rails client?

However, this is a Rails solution that has fancy Rails time helpers such as 20.minutes.from_now . You can set the expiration date to a specific date by adding a certain number of seconds at the current time, for example Time.now.to_i + (20 * 60) , or include ActiveSupport time helpers in your ruby โ€‹โ€‹script with require 'active_support/core_ext/numeric/time' . This will work with 20.minutes.from_now .

In addition, you will need the entire aws-sdk stone, not just the S3 part.

+7
source

All Articles