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
질문됨 일 년 전768회 조회
1개 답변
0
수락된 답변

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

profile pictureAWS
전문가
kentrad
답변함 일 년 전
profile pictureAWS
전문가
검토됨 일 년 전
  • I didn't realize if I change ContentType that it would generate a Content-Type header

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠