33

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?

3 Answers 3

80

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
4
  • Thanks! Was in the middle of having to write a whole script to do just this
    – schmoove
    Jan 30, 2018 at 20:38
  • 1
    Thanks, this really helped. However, when running in Windows AWS CLI, please be sure to remove the quotes from --exclude and --include option values, as they will be ignored - this caused the CLI to just copy all the files in my S3 regardless of their extension. Once I did --exclude * --include *.png it worked great.
    – Z-Bone
    May 9, 2018 at 10:24
  • 1
    Here is the official document: "If REPLACE is used, the copied object will only have the metadata values that were specified by the CLI command. Note that if you are using any of the following parameters: --content-type, content-language, --content-encoding, --content-disposition, --cache-control, or --expires, you will need to specify --metadata-directive REPLACE for non-multipart copies if you want the copied objects to have the specified metadata values." ( docs.aws.amazon.com/cli/latest/reference/s3/cp.html )
    – Manan Shah
    Dec 6, 2019 at 11:04
  • This does not changes the content type of S3 object versions. Please suggest. Dec 24, 2022 at 15:26
8

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"
1
  • Cool, but first I somehow need to select all objects with a certain extension like .jpg
    – bart
    Dec 7, 2016 at 14:26
7
aws 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 bucket
  • awk '{print substr($0, index($0, $4))}' => This will remove extra information like date and just give you all list of keys
  • grep <FILE_EXTENSTION> => This will filter the extension(say pdf/jpg) for which you want to update the content-type
  • 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 replace all the filtered files in your bucket with the content-type you specify
2
  • Is there a way to not show the output and having to exit every update?
    – alegria
    Feb 9, 2021 at 23:51
  • 1
    could redirect copy-object command to /dev/null
    – alegria
    Feb 10, 2021 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.