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.

wz2b
asked 17 days ago24 views
1 Answer
0
Accepted Answer

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

profile picture
EXPERT
kentrad
answered 16 days ago
profile picture
EXPERT
reviewed 14 days ago
  • I didn't realize if I change ContentType that it would generate a Content-Type header

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions