Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to upload image on amazon s3 using nodejs and make it public

I am using the following code to upload the image on amazon S3, but it is not public. What else parameter do I need to pass to make it public.

var AWS = require('aws-sdk');
AWS.config.region = 'us-west-2';

var s3bucket = new AWS.S3({params: {Bucket: 'myBucket'}});
s3bucket.createBucket(function() {
var params = {Key: 'myKey', Body: 'Hello!'};
s3bucket.upload(params, function(err, data) {
if (err) {
  console.log("Error uploading data: ", err);
} else {
  console.log("Successfully uploaded data to myBucket/myKey");
}
});
});
like image 224
Himanshu Raj Avatar asked Mar 04 '16 04:03

Himanshu Raj


People also ask

Can anyone upload to a public S3 bucket?

Before you can upload files to an Amazon S3 bucket, you need write permissions for the bucket. For more information about access permissions, see Identity and access management in Amazon S3. You can upload any file type—images, backups, data, movies, etc.


2 Answers

Make resource public when upload data at s3 bucket. I can able to download that link from any where.

Just try it:

var s3bucket = new AWS.S3({
  region: Constant.AWS_S3_REGION
 });
 console.log(filename);
 var params = {
  Key: filename,
  Body: fs.createReadStream(filepath),
  Bucket: Constant.AWS_S3_BUCKET,
  ACL:'public-read-write'
 };
s3bucket.upload(params, cb)

Cheers...!!!!

like image 151
Siten Avatar answered Oct 13 '22 08:10

Siten


For making data public in your bucket, convert following :

var params = {Key: 'myKey', Body: 'Hello!'};

To following:

var params = {Key: 'myKey', Body: 'Hello!', ACL: 'public-read'};

For better understanding follow This Good Tutorial

Happy Helping!

like image 36
Zeeshan Hassan Memon Avatar answered Oct 13 '22 08:10

Zeeshan Hassan Memon