Search code examples
node.jsamazon-s3acl

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?


Solution

  • 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