【问题标题】:Unable to assign ACL permissions to uploaded file when using S3FS node js lib使用 S3FS 节点 js lib 时无法为上传的文件分配 ACL 权限
【发布时间】:2017-05-22 19:40:40
【问题描述】:
我正在使用 S3FS 库(在 node.js 应用程序中)将文件上传到我的 AWS S3 目录。 (参考文档https://www.npmjs.com/package/s3fs)和stackoverflow answer here
s3fs 对象初始化
s3fsImpl = new s3fs(bucketPath, {
accessKeyId: xxxxx,
secretAccessKey: xxxxx,
ACL: 'public-read'
})
方法writeFile:s3fsImpl.writeFile(fileName, stream)
文件已成功上传,我收到“ETag”作为响应。但是,上传的文件无法公开访问。
如何授予适当的 ACL 权限以使其可供公众阅读?
【问题讨论】:
标签:
node.js
amazon-s3
acl
【解决方案1】:
您可以将 ACL 作为writeFile 方法中的第三个选项提供为
s3fsImpl.writeFile(fileName, stream,{ ACL: 'public-read'})
【解决方案2】:
实际上,我可以在节点中使用以下方法来实现这一点
第 1 步:
安装并包含“aws-sdk”以访问 aws 资源(我将它用于 S3)
var AWS = require('aws-sdk')
var s3 = new AWS.S3()
第 2 步:
s3.putObject({
Bucket: AWS_BUCKET_NAME, // bucket name on which file to be uploaded
Key: uploadKey, // file name on s3
ContentType: file.type, // type of file
Body: new Buffer(fileData, 'binary'), // base-64 file stream
ACL: 'public-read' // public read access
}, function (err, resp) {
if (err) { // if there is any issue
console.error('failed file upload to S3:: ', err)
callback(err, null)
}
console.log('response from S3: ', resp)
callback(null, 'Success')
}
)
ACL: 'public-read' 将为您在 S3 上的媒体/文件分配公共读取访问权限