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...
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...
RFC-6266似乎指定文件名是带引号的字符串。
ContentDisposition: 'attachment; filename="some_file_name.jpg"'
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}"`,
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.