Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I set content-type when creating a signed url for an object in s3 aws?

I want to create a signed url with a custom content-type, I was trying this:

s3.getSignedUrl('getObject', {Bucket: AWS_BUCKET_NAME, Key: 'myObjectsKey', ContentType: 'image/png'}, function (err, url) {
    console.log(err, url);
});

however this gives the error:

{ [UnexpectedParameter: Unexpected key 'ContentType' found in params]
  message: 'Unexpected key \'ContentType\' found in params',
  code: 'UnexpectedParameter',
  time: Thu Dec 18 2014 01:38:19 GMT-0400 (AST) }

Which I find strange because the documentation on signing requests here:

http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html

states that a signature is made from hashing:

Signature = URL-Encode( Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of( StringToSign ) ) ) );

StringToSign = HTTP-VERB + "\n" +
    Content-MD5 + "\n" +
    Content-Type + "\n" +
    Expires + "\n" +
    CanonicalizedAmzHeaders +
    CanonicalizedResource;  

Which allows you to give a content-type. Does anyone know what's going on?

like image 619
David Zorychta Avatar asked Dec 11 '22 02:12

David Zorychta


1 Answers

This must have been updated because you can now do this, just use ResponseContentType rather than ContentType. Check out the GET Object Docs.

const params = {
  Bucket: bucket, 
  Key: key, 
  Expires: 60, 
  ResponseContentType: 'image/png'
};

s3.getSignedUrl('getObject', params, function (err, url) {
  if (url) callback(null, url);
  else     callback(err, null);
});

I tested it even with Saz files and it worked great.

like image 130
cameck Avatar answered Jan 28 '23 14:01

cameck