简体   繁体   English

如何使用 AW S3 SDK 的 createPresignedPost 方法公开访问 AWS S3 文件?

[英]How to make an AWS S3 file public accessible using AW S3 SDK's createPresignedPost method?

I have a use case to keep the AWS S3 Bucket Private as default but,我有一个用例将 AWS S3 Bucket Private 保留为默认值,但是,
Make certain objects Public while uploading to AWS S3.在上传到 AWS S3 时将某些对象设为公开。

I am using the following code to sign the AWS S3 url using and ACL setting as public-read -我正在使用以下代码来签署 AWS S3 url 使用和 ACL 设置为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;
};

Request while uploading -上传时请求 -

在此处输入图片说明

I am able to upload the file to AWS S3, if I remove the conditions array in signing params,我可以将文件上传到 AWS S3,如果我删除了签名参数中的条件数组,
but the file is still not public when I click its url.但是当我单击其 url 时,该文件仍未公开。
I believe I have done something wrong code on signingParams part.我相信我在signingParams部分做了一些错误的代码。


Ref -参考 -
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createPresignedPost-property https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createPresignedPost-property

Upload file to s3 with POST 使用 POST 将文件上传到 s3

The order of parameters matters here.参数的顺序在这里很重要。 Put acl parameter before the file and it should work;将 acl 参数放在文件之前,它应该可以工作; otherwise S3 just ignores the value you provided.否则 S3 只会忽略您提供的值。

Below are the example screenshots with different placement of parameters in form-data.以下是在表单数据中参数位置不同的示例屏幕截图。

Also, be sure to give execute the createPresignedPost by a user with s3:PutObjectAcl and s3:PutObject permissions.此外,请确保由具有s3:PutObjectAcls3:PutObject权限的用户执行 createPresignedPost。

The correct order of form-data parameters表单数据参数的正确顺序

The same request but with acl parameter being placed after file (Ignored by S3)相同的请求,但acl参数放在file之后(被 S3 忽略)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用节点、createPresignedPost 和获取将图像文件直接从客户端上传到 AWS S3 - How to upload an image file directly from client to AWS S3 using node, createPresignedPost, & fetch S3:如何使用aws-sdk在nodejs中使用S3上传大文件 - S3 : How to upload a large file using S3 in nodejs using aws-sdk 使用适用于节点的AWS开发工具包将S3资源设为只读公开一段时间吗? - Make a S3 resource public read-only for some duration using the AWS SDK for node? 如何使用aws-sdk将文件同步上传到S3? - How to synchronously upload files to S3 using aws-sdk? 如何使用 aws-sdk 在 AWS S3 上获取文件的 URL? - How to get the URL of a file on AWS S3 using aws-sdk? 如何使用 AWS 开发工具包从 Amazon S3 下载文件后删除文件 - How to delete a file after downloading it from Amazon S3 using the AWS SDK 如何使用 aws-sdk v3 从 S3 保存文件 - How to save file from S3 using aws-sdk v3 如何使用他们的 sdk 在 AWS S3 中移动(而不是复制)文件? - How can I move (not copy) a file in AWS S3 using their sdk? Nodejs AWS S3 createPresignedPost返回错误无法读取未定义的属性(读取'endsWith') - Nodejs AWS S3 createPresignedPost returns error Cannot read properties of undefined (reading 'endsWith') 如何使 AWS S3 请求可重试? - How to make AWS S3 request retryable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM