Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change content type of Amazon S3 Objects

Tags:

The objects in my Amazon S3 bucket are all of the content type application/octet-stream. Some of these objects are PDFs, sometimes images like JPG, GIF, PNG. How can I change the content type of these objects to images/jpeg, application/pdf etc.?

Can it be done in batch through the Amazon Console?

Can I use the command line?

Or maybe through PHP?

like image 692
bart Avatar asked Dec 07 '16 10:12

bart


People also ask

How do I change the format of a S3 bucket?

You cannot. The default content-type is "application/octet-stream". The only option you have is setting the content-type at the time of upload or updating it once the upload is complete.

Can S3 files be edited?

s3 doesn't support editing files... it stores objects that must be rewritten any time you want to update them.

What format files are stored in S3?

Amazon S3 objects are CSV or delimited text files. All fields in an Amazon S3 file are of string data type with a data format that you cannot change and with a defined precision of 256. Data in Amazon S3 text files is written in String 256 format.


2 Answers

This is how you can set Content-Type for all files of type *.png

aws s3 cp \        s3://BUCKET-NAME/ \        s3://BUCKET-NAME/ \        --exclude '*' \        --include '*.png' \        --no-guess-mime-type \        --content-type="image/png" \        --metadata-directive="REPLACE" \        --recursive 
like image 184
Pavel Bely Avatar answered Sep 20 '22 16:09

Pavel Bely


You can use AWS sdk (php or other) I'll show how it works using CLI. To change content type on S3 given object, you need to copy this object and update the metadata information using the REPLACE tag so it copies to itself, you can achieve that using http://docs.aws.amazon.com/cli/latest/reference/s3api/copy-object.html

aws s3api copy-object --bucket <bucket_name> \     --content-type "images/jpeg" \     --copy-source <bucket_name>/path/to/images.jpeg \     --key path/to/images.jpeg \     --metadata-directive "REPLACE" 
like image 45
Frederic Henri Avatar answered Sep 22 '22 16:09

Frederic Henri