Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(nodeJS) Content-Type for S3 object: manually set to 'image/jpeg' but in S3 console comes up as 'application/octet'

I have a JPEG buffer which uploads and downloads successfully from S3. However, I'm trying to send it over the Messenger API, and when it's accessed programmatically Messenger throws errors because according to the S3 console, the actual Content-Type of the image is application/octet-stream.

My manually entered metadata appears under x-amz-meta-content-type. According to the AWS documentation, this is the default behavior. How might I override it to get image/jpeg under Content-Type?

My code:

var s3 = new AWS.S3();
var params = {
  Body: buffer,
  Bucket: <bucket>,
  Key: <key>,
  Metadata: {
    'Content-Type': 'image/jpeg'
  }
};
s3.putObject(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else  {
    console.log(data);
  }
})
like image 363
user61871 Avatar asked Nov 03 '17 13:11

user61871


1 Answers

Don't set it in the Metadata section, that's only for properties that will be prefixed with x-amz-meta. There is a ContentType parameter at the main level, like so:

var s3 = new AWS.S3();
var params = {
  Body: buffer,
  Bucket: <bucket>,
  Key: <key>,
  ContentType: 'image/jpeg'
};
s3.putObject(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else  {
    console.log(data);
  }
})
like image 79
Mark B Avatar answered Oct 31 '22 23:10

Mark B