繁体   English   中英

使用 S3FS 节点 js lib 时无法为上传的文件分配 ACL 权限

[英]Unable to assign ACL permissions to uploaded file when using S3FS node js lib

我正在使用 S3FS lib(在 node.js 应用程序中)将文件上传到我的 AWS S3 目录。 (参考文档https://www.npmjs.com/package/s3fs )和stackoverflow 在这里回答

s3fs 对象初始化
s3fsImpl = new s3fs(bucketPath, { accessKeyId: xxxxx, secretAccessKey: xxxxx, ACL: 'public-read' })

方法 writeFile: s3fsImpl.writeFile(fileName, stream)
文件已成功上传,我收到“ETag”作为响应。 但是,上传的文件不可公开访问。

如何授予适当的 ACL 权限以使其可供公众阅读?

您可以提供 ACL 作为writeFile方法中的第三个选项

s3fsImpl.writeFile(fileName, stream,{ ACL: 'public-read'})

实际上,我能够在节点中使用以下方法来解决这个问题

第 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 上的媒体/文件的公共读取访问权限

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM