Nona
Nona

Reputation: 5462

How to unexpire an Amazon S3 file url?

So I built a web application in Ruby on Rails that uploads file to Amazon S3. I stored a document link in the database such as below.

https://example.s3.amazonaws.com/mybucket/random_file.pdf

But when I try to access it I get the below error. How do I unexpire the random_file.pdf link programatically using the Ruby aws-sdk gem? (or do I have to do it via the Amazon S3 console in the browser?)

<Error>
<Code>AccessDenied</Code>
<Message>Request has expired</Message>
<Expires>2014-12-24T06:09:09Z</Expires>
<ServerTime>2015-03-12T19:12:38Z</ServerTime>
<RequestId>03BlahBlah</RequestId>
<HostId>
randomstring
</HostId>
</Error>

Upvotes: 0

Views: 535

Answers (1)

Trevor Rowe
Trevor Rowe

Reputation: 6528

The url you gave is a public, non-signed url. If you intend for users to access objects in your S3 bucket annonymously, then you need to store them with a public-read ACL.

Depending on the tool you use to upload the file to Amazon S3, you can usually specify a canned public-read acl as part of the upload process. Using v2 of the aws-sdk gem:

s3 = Aws::S3::Resource.new
obj = s3.bucket('name').object('key')
obj.upload_file('/source', acl:'public-read')
obj.public_url
#=> "https://..."

You will now be able to access the object via any normal HTTP GET request, such as with a browser.

Upvotes: 1

Related Questions