10

I am streaming files from S3 and through my API to the client in order to enforce access control rules. In doing so, I need to set the content-type header appropriately. Does anyone know of a way to get the content-type from S3 without making a separate call to headObject? Right now I have to first get the object metadata and then make another request to get the object stream.

EDIT: To clarify, I'm using

return s3.getObject(params).createReadStream();

to get the stream, so there is no callback that I'm aware of.

cava23
  • 477
  • 1
  • 8
  • 16
  • A `GET` request returns exactly the same metadata in the headers that you get from a `HEAD` request... so it seems like you already should have what you are looking for, in data.ContentType in the callback from getObject. Wouldn't you? – Michael - sqlbot Feb 13 '15 at 11:04
  • take a look at https://stackoverflow.com/questions/35782434/streaming-file-from-s3-with-express-including-information-on-length-and-filetype?rq=1 – killdash9 Nov 25 '19 at 23:06
  • you can get the content type from the data returned by the getObject method: here's an example from the official documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property:~:text=To%20retrieve%20a%20byte%20range%20of%20an%20object – Kamèl Romdhani Aug 23 '23 at 12:36

1 Answers1

-6

S3 will save the object with Content-Type that you specify while save your data by calling the putObject function.

s3Instance.putObject({
  Bucket: "my-bucket",
  ContentType: "application/xml",
  Body: xmlContent
}, function(err) {
  //callback here
});

Source: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property

The Content-Type of the return object while calling the getObject function will be the same that you specify above. However you can set the Content-Type that you desire while calling the getObject function:

s3Instance.getObject({
  Bucket: "my-bucket",
  Key: "MyXMLFile",
  ContentType: "text/xml"
}, function(err, data) {
  //callback here
});

Source: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property

Srivathsa
  • 941
  • 1
  • 10
  • 27