如何使用AW S3 SDK的createPresignedPost方法公开访问AWS S3文件? [英] How to make an AWS S3 file public accessible using AW S3 SDK's createPresignedPost method?
问题描述
我有一个用例将AWS S3存储桶设为默认私有,但是,
在上传到AWS S3时,将某些对象设为Public.
I have a use case to keep the AWS S3 Bucket Private as default but,
Make certain objects Public while uploading to AWS S3.
我正在使用以下代码使用ACL设置和public-read
-
I am using the following code to sign the AWS S3 url using and ACL setting as public-read
-
module.exports.generateS3PostSignedUrl = async (bucketName, bucketKey, objectExpiry) => {
let s3Client = new AWS.S3({
region: 'some-region'
});
let signingParams = {
Expires: objectExpiry,
Bucket: bucketName,
Fields: {
key: bucketKey,
},
Conditions: [
['acl', 'public-read']
],
ACL: 'public-read'
}
let s3createPresignedPost = util.promisify(s3Client.createPresignedPost).bind(s3Client);
let signedUrl = await s3createPresignedPost(signingParams);
return signedUrl;
};
上传时请求-
如果我删除了签名参数中的conditions数组,则能够将文件上传到AWS S3,
但是当我单击它的URL时,该文件仍然不公开.
我相信我在signingParams
部分执行了错误的代码.
I am able to upload the file to AWS S3, if I remove the conditions array in signing params,
but the file is still not public when I click its url.
I believe I have done something wrong code on signingParams
part.
参考-
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createPresignedPost属性
Ref -
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createPresignedPost-property
推荐答案
此处的参数顺序很重要.将acl参数放在文件之前,它应该可以工作;否则S3只会忽略您提供的值.
The order of parameters matters here. Put acl parameter before the file and it should work; otherwise S3 just ignores the value you provided.
下面是在屏幕表格数据中参数位置不同的示例屏幕截图.
Below are the example screenshots with different placement of parameters in form-data.
此外,请确保由具有s3:PutObjectAcl
和s3:PutObject
权限的用户执行createPresignedPost.
Also, be sure to give execute the createPresignedPost by a user with s3:PutObjectAcl
and s3:PutObject
permissions.
相同的请求,但在file
之后放置了acl
参数(被S3忽略) )
The same request but with acl
parameter being placed after file
(Ignored by S3)
这篇关于如何使用AW S3 SDK的createPresignedPost方法公开访问AWS S3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!