Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the default content type on multiple files that have been uploaded to a AWS S3 bucket

Using aws-cli I uploaded 5gb of files to an Amazon S3 bucket that I have made a static website. Some of the files the site references are .shtml files, but S3 has defaulted to a metadata content type of binary/octet-stream but I want those files to have a metadata content-Type of text/html. Otherwise it doesn't work in the browser.

Is there a aws-cli s3api command I can use to change the content type for all files with a .shtml extension?

like image 324
Albert Still Avatar asked Dec 11 '22 23:12

Albert Still


2 Answers

You can set content type on specific file types like the following.

"aws s3 sync ${BASE_DIR} s3://${BUCKET_NAME} --exclude *.shtml"
"aws s3 sync ${BASE_DIR} s3://${BUCKET_NAME} --exclude '*' --include '*.shtml' --no-guess-mime-type --content-type text/html"
like image 194
Eli Web-Dev Avatar answered Apr 27 '23 21:04

Eli Web-Dev


To modify the metadata on an Amazon S3 object, copy the object to itself and specify the metadata.

From StackOverflow: How can I change the content-type of an object using aws cli?:

$ aws s3api copy-object --bucket archive --content-type "application/rss+xml" \
    --copy-source archive/test/test.html --key test/test.html \
    --metadata-directive "REPLACE"
like image 36
John Rotenstein Avatar answered Apr 27 '23 20:04

John Rotenstein