Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 SDK: Change filename on download?

Is this the correct way to generate a URL and change it's download name?

$s3 = new AmazonS3();
$opt =  array('response' => array('Content-Disposition' => 'attachment; "filename=newname.txt"'));
$url = $s3->get_object_url('bucket', 'file.txt', '5 minutes', $opt));

Apparently doesn't work with me.

like image 970
IMB Avatar asked Feb 08 '12 19:02

IMB


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.

How do you upload the same name file to Amazon S3 and overwrite?

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.

Can you overwrite a file in S3?

Simply upload your new file on top of your old file to replace an old file in an S3 bucket. The existing file will be overwritten by your new file.

Can you rename an S3 object?

Your answer Yes, There's no option to rename bucket functionality for S3 because there are technically no folders in S3 so we have to handle every file within the bucket. Create a new bucket, copy the contents from the new bucket and delete the old bucket.


1 Answers

After several tests, apparently get_object_url requires Content-Disposition parameter to be in lower case.

Note that this is not the case for create_object which works case-insensitive.

So the working code for above is:

$opt =  array('response' => array('content-disposition' => 'attachment; "filename=newname.txt"'));
like image 168
IMB Avatar answered Oct 01 '22 21:10

IMB