Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change filename for user when downloading directly from AWS S3

I am developing an application where I will be storing files on S3. Due to the fact that filenames can be different I have to rename the files to something generic in the bucket so I know which one to get at a later date.

If I was to allow the user to download through my web app, I could change the filename back before sending it on to the user. However I want the user to directly download the file using a temporary secure token such as below.

s3 = AWS::S3.new(
  :access_key_id => 1234,
  :secret_access_key => abcd
)
object = s3.buckets['bucket'].objects['path/to/object']
object.url_for(:get, { :expires => 20.minutes.from_now, :secure => true }).to_s

But how do I change the filename the user sees when downloading this file back to their original filename?

like image 287
Adam Avatar asked Jan 09 '14 02:01

Adam


People also ask

How do I rename a file in Amazon S3?

There is no direct method to rename the file in s3. what do you have to do is copy the existing file with new name (Just set the target key) and delete the old one.

Does S3 replace files with same name?

By default, when you upload the file with same name. It will overwrite the existing file. In case you want to have the previous file available, you need to enable versioning in the bucket.

Does S3 support rename?

Things changed recently with an introduction of copy enhancements that support objects, and we decided to take Amazon's lead and support copy files and folders in MSP360 Explorer for Amazon S3. All you have to do is to select a file or folder you want to rename and click on the “Rename” button on the toolbar.

How do I download data from AWS S3?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.


1 Answers

Set the S3 object's Content-disposition property to attachment; filename="nameyouwant" where nameyouwant is, of course, whatever name you want the downloaded file to have by default. When the user downloads from S3 using a web browser, the browser will use the name in the Content-disposition header instead of the URL.

like image 54
Charles Engelke Avatar answered Oct 12 '22 11:10

Charles Engelke