亚马逊网络服务-AWS S3 node.js SDK上传的文件和文件夹权限

我正在使用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.');
        })

});

我需要使上传的文件可以通过Cloudfront下载,如果我确定正确的话,我需要设置文件权限:每个人都可以打开/下载,Folder2应该公开(通过菜单“公开”)。 所以有两个问题:

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

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

WHITECOLOR asked 2020-02-21T12:23:02Z
2个解决方案
80 votes

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

需要在putObject中添加选项:

ACL:'public-read'
WHITECOLOR answered 2020-02-21T12:23:20Z
8 votes

这是一个工作代码段,该代码段将本地文件(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);
    }
});
user1032613 answered 2020-02-21T12:23:40Z
translate from https://stackoverflow.com:/questions/14375895/aws-s3-node-js-sdk-uploaded-file-and-folder-permissions