0

In my application, I am using S3 to store files. But when retrieving those files, they are opening in new tab. But, I want to download those files. Following some references and AWS documentation, I came to know that we can achieve that using ContentDisposition metadata parameter. I did as following:

s3.copyObject({
   CopySource: object.bucket + '/' + object.key,
   Bucket: BUCKET,
   Key: object.destKey,
   ContentDisposition: 'attachment; filename=some_file_name.jpg'
})

When I do as above, the ContentDisposition metadata parameter is not being added (I checked in AWS S3 console). I am not sure where I am doing wrong. Please help me solve this.

Thanks...

Sai M.
  • 2,548
  • 4
  • 29
  • 46

3 Answers3

1

RFC-6266 appears to specify that the filename is a quoted-string.

ContentDisposition: 'attachment; filename="some_file_name.jpg"'
Community
  • 1
  • 1
Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427
1

I ran into a similar issue, but using AWS SDK for JavaScript v3. It appears that if you use the Copy command, and want to set the ContentDisposition header, you must also set the MetadataDirective (corresponding to the x-amz-metadata-directive header) to "REPLACE". Otherwise, the metadata from the source object will be copied, and if that does not include a ContentDisposition, or an incorrect one for the target object, it will still be applied for the target object.

new CopyObjectCommand({
    Bucket: bucket,
    CopySource: `${bucket}/${sourceKey}`,
    Key: targetKey,
    MetadataDirective: 'REPLACE',
    ContentDisposition: `attachment; filename="${filename}"`,
});
eckberg
  • 66
  • 3
0

I don't know why it was not working. But, after a lot of research I came across this post. And when getting those objects, I sent parameters like following

return s3utils.getSignedUrl({
  filePath: key,
  type: 'getObject',
  "ResponseContentDisposition": 'attachment'
});

Using ResponseContentDisposition parameter did the trick for me. But, still not sure why copyObject with ContentDisposition is not working.

Still question is open

Thanks...

Sai M.
  • 2,548
  • 4
  • 29
  • 46
  • The difference from the copy command is that getSignedUrl() will generate the ContentDisposition "on-the-fly" using this approach. Hence, the header will not be saved to S3 for future use. It might well be a sufficient solution when this is the method you use for downloading objects, but will not work if you, e.g., use CloudFront (CDN) in between. – eckberg Aug 13 '21 at 11:28