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 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.htmlaws s3 ls s3://<YOUR_BUCKET> --recursive | awk '{print substr($0, index($0, $4))}' | grep <FILE_EXTENSTION> | while read -r line; do aws s3api copy-object --bucket <YOUR_BUCKET> --acl "<ACL_POLICY_IF_NEEDED>" --content-type "<REQUIRED_CONTENT_TYPE>" --copy-source "<YOUR_BUCKET>/$line" --key "$line" --metadata-directive "REPLACE"; done
This will do the trick. Breaking down the commands:
aws s3 ls s3://<YOUR_BUCKET> --recursive
=> This lists all files in your bucketawk '{print substr($0, index($0, $4))}'
=> This will remove extra information like date and just give you all list of keysgrep <FILE_EXTENSTION>
=> This will filter the extension(say pdf/jpg) for which you want to update the content-typewhile read -r line; do aws s3api copy-object --bucket <YOUR_BUCKET> --acl "<ACL_POLICY_IF_NEEDED>" --content-type "<REQUIRED_CONTENT_TYPE>" --copy-source "<YOUR_BUCKET>/$line" --key "$line" --metadata-directive "REPLACE"; done
=> This will replace all the filtered files in your bucket with the content-type you specifyThis is how you can set
Content-Type
for all files of type *.png