88

I'm uploading file to S3 using aws-sdk package:

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.');
        })

});

I I need to make uploaded file to be downloadable via cloudfront, if I asume right, I need to set permissions on file: Everyone Open/Download, Folder2 should be made public (via menu Make Public). So 2 questions:

1) How to set\modify permissions on uploaded file\folder?

2) How make Folder public using AWS SDK for node.js.

3 Answers 3

152

Found it http://docs.aws.amazon.com/AmazonS3/latest/dev/ACLOverview.html#CannedACL

need to add option in putObject or upload:

ACL:'public-read'
5
  • Is that an option you put in with the first object you passed into putObject? Meaning, was it a property alongside Bucket, Key, and Body?
    – juanpaco
    Commented Jun 25, 2015 at 14:38
  • 6
    I had the above question too, so just to answer it for those that may have it as well, ACL:'public-read' should be added in with the Bucket, Key and Body object that is sent to S3. Thanks to @WHITECOLOR for the help!
    – julianwyz
    Commented Dec 14, 2015 at 2:54
  • 1
    I assume you found an answer at some point, but in case you didn't, yes, you pass the ACL property along with 'Bucket' and 'Key'. Commented Mar 9, 2016 at 17:10
  • 12
    Note that the IAM permission "s3:PutObjectACL" must be included in the appropriate policy. Commented Apr 6, 2016 at 17:09
  • 2
    any idea why there would be access denied when the user has the permission AmazonS3FullAccess ? Commented Apr 21, 2019 at 17:24
45

The following works like a charm. (Note the ACL key in params)

const params = {
  Bucket: bucketName + path,
  Key: key,
  Body: buffer,
  ContentEncoding: 'base64',
  ContentType: 'image/jpeg',
  ACL:'public-read'
};
await s3.putObject(params).promise();

Note: IAM permission "s3:PutObjectACL" must be included in the appropriate policy otherwise you will get Access Denied errors.

26

Here is a working code snippet that uploads a local file (test-file.gif) to S3 bucket and prints out a URL for everyone to download.

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);

// Upload the file to a 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);
    }
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.