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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With