尝试将元数据添加到我的预签名网址。 url 生成并在查询字符串中包含元数据,但我无法提交带有元数据的文件,因为它因 403 禁止而失败。应该注意的是,如果我从设置中删除元数据,则该 url 可以正常工作。 我尝试过签署标头、取消标头,但没有任何效果。我知道这可以通过预先签名的 POST 实现,但我需要它才能用于 PUT。
用于生成 URL 的 Lambda 代码:
const params={
Bucket: bucket,
Key: key,
Metadata:{
"filesize": filesInfo.fileSize,
"origfilename": encodeURIComponent(filesInfo.origFilename),
"filetype": filesInfo.fileType,
},
};
console.log("params=", params);
const headersToSign= new Set([
// "ContentLength",
"x-amz-meta-fileSize",
"x-amz-meta-fileType",
"x-amz-meta-origFilename",
]);
const unhoistableHeaders=new Set ([
"x-amz-meta-fileSize", "x-amz-meta-fileType", "x-amz-meta-origFilename"
]);
const command=new PutObjectCommand(params);
const url = await getSignedUrl(s3Client, command, {
expiresIn: expiry,
signedHeaders: headersToSign,
unhoistableHeaders: unhoistableHeaders
});
客户端获取:
const encodedFileName = encodeURIComponent(fileToSubmit.name);//did this to ensure filenames work--need to decode when showing file
const myHeaders = new Headers({
"x-amz-meta-filesize": fileToSubmit.size,
"x-amz-meta-filetype": fileToSubmit.type,
"x-amz-meta-origfilename": encodedFileName,
});
const myRequest = new Request(url, {
method: 'PUT',
mode: 'cors',
cache: 'default',
body: fileToSubmit,
headers: myHeaders
});
fetch(myRequest)
.then(response => {
})
.then(data => {
})
那太愚蠢了。现在可以了。我只是没有确保我的元数据值都是字符串。文件大小元数据是一个数字,一旦我将其转换为 lambda 中的字符串并将该值添加到元数据中,一切都很好。