amazon-web-servicesamazon-s3s3cmd

Change meta-data on selected files in S3


I found a bunch of image files that has the wrong extension. Due to the way the website is made, they must have a .jpg extension. But some of them are png files.

So I made a quick list of fake JPEG files ls public/assets/image/*.jpg | xargs file --mime | grep -v jpeg and found https://stackoverflow.com/a/25825362/205696 that has an example of how to use the aws-cli tool to change the Cache-Control header (I want to change the Content-Type header).

According to the aws s3 sync help man page, the syntax is sync <LocalPath> <S3Uri> or <S3Uri> <LocalPath> or <S3Uri> <S3Uri>. Since I do not want to sync any files from my machine, I opt for the last syntax aws s3 sync <S3Uri> <S3Uri>.

However doing:

aws s3 sync s3://firefund-assets/underathens4.jpg s3://firefund-assets/underathens4.jpg --content-type image/png

does not seem to change anything. The underathens4.jpg file still has System defined Content-Type image/jpeg... and there is nothing written to stdout.

If I can change the meta-data for a file in S3, then I can simply do cat fakeJpegs.txt | sed 's/public\/assets\/image\///' | xargs -i aws s3 sync s3://firefund-assets/'{}' s3://firefund-assets/'{}' --content-type image/png

I do not want to re-upload any files. In that case, I could use s3.console.aws.amazon.com and do the change manually. I just want to have a little script to do this occasionally..

Change the default content type on multiple files that have been uploaded to a AWS S3 bucket is related but suggest uploading the files again.


Solution

  • You can copy an object over itself while specifying new information.

    Use the cp command rather than the sync command:

    aws s3 cp s3://firefund-assets/file.jpg s3://firefund-assets/file.jpg --content-type image/png