AWS S3 node.js SDK 上传文件和文件夹权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14375895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:58:16  来源:igfitidea点击:

AWS S3 node.js SDK uploaded file and folder permissions

node.jsamazon-web-servicesamazon-s3

提问by WHITECOLOR

I'm uploading file to S3 using aws-sdk package:

我正在使用 aws-sdk 包将文件上传到 S3:

fs.readFile(sourceFile, function (err, data) {
    if (err) { throw err; }

    s3.client.putObject({
        Bucket: bucketName,
        Key: 'Folder/image.jpg',
        Body: data
    }, function (res) {
            console.log('Successfully uploaded file.');
        })

});

I I need to make uploaded file to be downloadable via cloudfront, if I asume right, I need to set permissions on file: Everyone Open/Download, Folder2 should be made public (via menu Make Public). So 2 questions:

II需要使上传的文件可以通过cloudfront下载,如果我认为正确,我需要设置文件权限:Everyone Open/Download,Folder2应该公开(通过菜单Make Public)。所以2个问题:

1) How to set\modify permissions on uploaded file\folder?

1) 如何设置\修改上传文件\文件夹的权限?

2) How make Folder public using AWS SDK for node.js.

2) 如何使用适用于 node.js 的 AWS 开发工具包公开文件夹。

回答by WHITECOLOR

Found it http://docs.aws.amazon.com/AmazonS3/latest/dev/ACLOverview.html#CannedACL

找到它 http://docs.aws.amazon.com/AmazonS3/latest/dev/ACLOverview.html#CannedACL

need to add option in putObject:

需要在 putObject 中添加选项:

ACL:'public-read'

回答by user1032613

Here is a working code snippet that uploads a local file (test-file.gif) to S3 bucket and prints out a URL for everyone to download.

这是一个工作代码片段,它将本地文件 (test-file.gif) 上传到 S3 存储桶并打印出一个 URL 供所有人下载。

const fs = require('fs');
const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-west-1' });

// Fill in your bucket name and local file name:
const BUCKET_NAME = 'test-bucket-name-goes-here'
const FILE_NAME_LOCAL = './test-file.gif'
const FILE_NAME_S3 = 'this-will-be-the-file-name-on-s3.gif'
const FILE_PERMISSION = 'public-read'

// Create S3 service object
s3 = new AWS.S3({ apiVersion: '2006-03-01' });

// Get file stream
const fileStream = fs.createReadStream(FILE_NAME_LOCAL);

// Call S3 to retrieve upload file to specified bucket
const uploadParams = {
    Bucket: BUCKET_NAME,
    Key: FILE_NAME_S3,
    Body: fileStream,
    ACL: FILE_PERMISSION
};

s3.upload(uploadParams, function (err, data) {
    if (err) {
        console.log("Error", err);
    } if (data) {
        console.log("Upload Success", data.Location);
    }
});

回答by Ussama Zubair

The following works like a charm. (Note the ACL key in params)

下面的工作就像一个魅力。(注意 params 中的 ACL 键

const params = {
  Bucket: bucketName + path,
  Key: key,
  Body: buffer,
  ContentEncoding: 'base64',
  ContentType: 'image/jpeg',
  ACL:'public-read'
};
await s3.putObject(params).promise();

Note: IAM permission "s3:PutObjectACL" must be included in the appropriate policy otherwise you will get Access Deniederrors.

注意:IAM 权限“ s3:PutObjectACL”必须包含在适当的策略中,否则您将收到拒绝访问错误。