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年前604ビュー
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

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ