简体   繁体   中英

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

I am using S3FS lib (in node.js app) to upload files to my AWS S3 directory. (referring documentation https://www.npmjs.com/package/s3fs ) and stackoverflow answer here

s3fs object init
s3fsImpl = new s3fs(bucketPath, { accessKeyId: xxxxx, secretAccessKey: xxxxx, ACL: 'public-read' })

method writeFile: s3fsImpl.writeFile(fileName, stream)
file is uploaded successfully and I am getting "ETag" in response. However, uploaded file is not accessible publicly.

How can I give proper ACL permissions to make it accessible for public-read?

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

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

Actually I was able to get this working with following approach in node

Step 1: Install and include "aws-sdk" to access aws resources (I used it for S3)

var AWS = require('aws-sdk')
var s3 = new AWS.S3()

Step 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' will assign public-read access to your media/file on S3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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