How do I presign url of PutObjectCommand with certain headers?

Viewed 485

I am new to aws-sdk and I want to sign Cache-Control, Content-Type and x-amz-acl headers for presigned url. Is it possible to do with s3-request-presigner? I can't find any example.

var command = new PutObjectCommand({
  Bucket: 'mybucket',
  Key: 'file.txt',
  ACL: 'public-read',
  CacheControl: 'public, max-age=1000',
  ContentType: 'text/plain',
  });

var signedUrl = await getSignedUrl(s3Client, command, {
  expiresIn: 3600, 
  signableHeaders: 
    new Set(['Cache-Control', 'Content-Type', 'x-amz-acl'])
}); 

The resulting url contains this: X-Amz-SignedHeaders=host. That is, my headers don't get signed. What do I have to do?

Also, can I restrain the size of the upload with something like content-length-range and sign it as well?

1 Answers

You have probably solved this by now, but what XHR client were you using?

I had the same issue using axios because I was calling axios like this:

axios({
    url: signedRequest,
    method: 'put',
    data: Body,
    headers: {
      'x-amz-acl': 'public-read-write',
      'Content-Type': 'application/pdf'
    },
    maxContentLength: Infinity,
    maxBodyLength: Infinity
  })

I needed to remove the headers property in this axios call because I was already setting ACL and ContentType when getting the signed request. Once I remove the headers property from my axios call, it started working.

Related