串AWS S3 SDK時
要下載一個項目,使用 Signature的下載方式
const getS3SignedDocumentURL = (docName) => {
const url = s3.getSignedUrl('getObject', {
Bucket: <aws-s3-bucket-name>,
Key: <aws-s3-object-key>,
Expires: <url-expiry-time>,
});
return url;
};
但會因為檔案預設的 Content-Type 被 Browser 處理(例如txt就直接打開)
所以需要指定一個 Content-Disposition
兩個方法
1.在網址後加上
String url = serverURL+'/'+bucketName+'/'+codedFilename+'?response-content-disposition='+EncodingUtil.urlEncode('attachment; filename=abc.doc','UTF-8')+'&AWSAccessKeyId='+awskey+'&Expires='+Lexpires+'&Signature='+codedsigned;
然後含這串signed的文字給後端去算signed
2.直接在Header裡加上ResponseContentDisposition: 原因是若後端已經拿算好的URI去計算Signed的話,只能靠在呼叫AWS s3 sdk 時加上ResponseContentDisposition
const getS3SignedDocumentURL = (docName) => {
const url = s3.getSignedUrl('getObject', {
Bucket: <aws-s3-bucket-name>,
Key: <aws-s3-object-key>,
Expires: <url-expiry-time>,
ResponseContentDisposition: `attachment; filename="${docName}"`
});
return url;
};
參考資料https://stackoverflow.com/questions/59684182/s3-presigned-url-multiple-content-disposition-headers
https://stackoverflow.com/questions/19046718/aws-s3-force-file-download-using-response-content-disposition
沒有留言:
張貼留言