How to change content type of Amazon S3 Objects

后端 未结 3 733
情书的邮戳
情书的邮戳 2020-12-06 05:06

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,

相关标签:
3条回答
  • 2020-12-06 05:46

    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
    0 讨论(0)
  • 2020-12-06 05:55

    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"
    
    0 讨论(0)
  • 2020-12-06 06:05

    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
    
    0 讨论(0)
提交回复
热议问题