I have a large S3 bucket with a nested "folder" structure containing (among other things) static .json and .md files. Theses files are being served by S3 as text/plain
rather than the correct application/json
and text/markdown
.
I have updated the bucket defaults so that new uploads will have the correct content type.
What is the best way to walk the "tree" and update the content type for files matching a certain extension?
Here is an example how to do this with the aws cli tool. The cp tool allows the use of a recursive options, which I don't think the s3api tool can do. In this case, I'm fixing a bunch of SVGs. Remove the --dryrun options when you are ready to unleash it.
After some digging, I found that the
s3cmd
tool can do this. For example, to set JSON files to application/json:Unless you want to get your hands dirty using boto/python or another AWS API, I suspect your best bet would be to use
$ aws s3api copy-object
with the--content-type
flag to copy an object to itself, setting the new content type.Here's the documentation for s3api.
It goes without saying that you should test this first, before running it recursively on your whole bucket.
As mentioned earlier, you can use
aws
tool by Amazon and use s3api to copy-object onto itself, and usemetadata-directive=REPLACE
to change the content-type.I am putting this here, because sometimes you would want to iterate on filenames that are stored in database, and this is how you can do it through cli.