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?
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.
s3 doesn't support editing files... it stores objects that must be rewritten any time you want to update them.
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.
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
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"
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