我使用我的應用程式成功地將檔案上傳到 S3。我使用我的服務器使用 aws-sdk v3 為我生成的 signedUrl 從瀏覽器直接上傳。
要獲取已標記的 URL,它看起來有點像這樣
const s3Params = {
Bucket : bucketName,
Key : fileName,
ContentType:fileType,
// Metadata:{'Content-Disposition':'attachment'}
// ContentDisposition:'attachment'
};
try {
const command = new PutObjectCommand(s3Params);
const url = await getSignedUrl(s3v3,command,{expiresIn:60});
return url;
} catch (e) {
console.log('************** there was an error signing th url');
console.log(e);
throw e;
}
};
這作業得很好,但是當我閱讀一些檔案時,我看到我應該能夠設定標題 ContentDisposition。在此檔案它說,PutObjectCommand的輸入從延伸PutObjectRequest
后者有一個可選引數,ContentDisposition因為我想將其設定為附件,以允許我為我的用戶提示“下載”視窗。但是,當我使用上述 signedURL 但添加該ContentDisposition:'attachment'欄位時,我收到一個禁止錯誤。
有誰知道我在這里遺漏了什么?這不是一個實際選項還是我需要為此修改我的 S3 權限中的某些內容?
uj5u.com熱心網友回復:
我們必須ContentDisposition為PutObjectCommandparam 和getSignedUrl函式指定 ,如下所示:
async function main(fileName, bucketName, fileType) {
const s3Params = {
Bucket: bucketName,
Key: fileName,
ContentType: fileType,
ContentDisposition: 'attachment'
};
const client = new S3Client({region: 'us-east-1'});
const command = new PutObjectCommand(s3Params);
const url = await getSignedUrl(client, command, {expiresIn: 60, ContentDisposition: 'attachment'});
const file = await fs.readFile(fileName);
const result = await axios({
method: 'put',
url,
data: file,
headers: {
'Content-Type': fileType,
'Content-Disposition': 'attachment'
}
});
return result;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/346554.html
標籤:节点.js 亚马逊网络服务 亚马逊-s3 aws-sdk 预签名网址
