How do you change S3 Content-Type metadata through the API?

0

This Document makes it clear that you can change the Metadata of an object. Through the console it works fine. I can't figure out how you're supposed to do it through the API.

I have a problem that has been asked before: CloudFront sourced by an S3 bucket, and if the Content-Type is wrong it does the wrong thing. When I upload an xyz.js file it labels the content as 'text/plain'. I'm doing this through a variety of clients including plain old s3cmd. I can specify the type on the s3cmd command line but I don't particularly want to - I'm trying to make this easy for people who are not me.

What I'm trying to do is an S3 - Lambda notification, in two steps:

  1. When I receive the S3CreateEvent, execute a HeadObjectCommand on that bucket/key to get the current metadata.
  2. In that response, look at GetObjectCommandOutput.Metadata and see if it has a key "content-type". If so, does it match? If it does not match, do a CopyObjectCommand but I set both ContentType and Metadata: { "content-type": "text/javascript" }

The trouble is, I never find an existing content-type in Metadata using the keys "Content-Type", "content-type", nor "contentType". I GUESS I could just do the CopyObjectCommand every time, but it seems better to check first and avoid any kind of recursion. It's not clear to me if CopyObjectCommand will trigger another notification but a test left me believing it does.

It's still weird to me that if you upload an .js file the default content-type seems to be text/plain. In fact, in the s3 console it shows up in two places: Type: js, and in the metadata it shows "System Generated, Content-Type, text/plain".

If I use `aws s3 cp junk2.js s3://mybucketname --content-type="text/javascript" it does the correct thing.

This problem is much discussed on stackoverflow but it's mostly just workarounds. There isn't a clear answer.

profile picture
wz2b
已提问 1 年前742 查看次数
1 回答
0
已接受的回答

Have you tried looking for 'ContentType'? See: Interface HeadObjectCommandOutput

profile pictureAWS
专家
kentrad
已回答 1 年前
profile pictureAWS
专家
已审核 1 年前
  • I didn't realize if I change ContentType that it would generate a Content-Type header

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则

相关内容