0

In S3 bucket every time I create a folder with the dynamic name and multiple images with private access permission that is fine, One image I need to grant permission as public access, I am using aws-sdk Javascript here my code for saving Image I need to give public access permission

const Aws = require('aws-sdk');
const Bucket_Name=process.env.BUCKET_NAME;
const IAm_user_Key=process.env.IAM_USER_KEY;
const IAm_user_Secret=process.env.IAM_USER_SECRET;
const uniqueID = getuniqueid();

///Setting AWS
Aws.config.update({ accessKeyId:IAm_user_Key,secretAccessKey:IAm_user_Secret,region:'ap-south-1' });

//Setting S3 Bucket
var s3Bucket = new Aws.S3({params: {Bucket: Bucket_Name}});

//Setting up poster 
var poster = new Buffer(data.poster.replace(/^data:image\/\w+;base64,/, ""),'base64');
var posterdata = { Key: (uniqueID+'/poster'), Body: poster, ContentEncoding: 'base64', ContentType: 'image/png'};
    saveImagetos3(posterdata);

function saveImagetos3(data){
        s3Bucket.putObject(data, function(err, data){
            if (err) { 
              console.log(err,"Error uploading image");
            } else {
              console.log('succesfully uploaded the image!');
            }
        });
    }

function getuniqueid(){
  //Some blah blah code for create random id
  return randomID;
}
Amit Shakya
  • 962
  • 3
  • 16
  • 30
  • 1
    Possible duplicate of [AWS S3 node.js SDK uploaded file and folder permissions](https://stackoverflow.com/questions/14375895/aws-s3-node-js-sdk-uploaded-file-and-folder-permissions) – Deiv Feb 20 '19 at 14:24
  • Thanks but I am facing error 'Access denied' while saving public read file, maybe bucket permission issue... – Amit Shakya Feb 20 '19 at 14:40
  • sorry I didn't find that solution but that dosen't work for me too, I feel maybe my IAM policy issue, here is my IAM policy { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListAllMyBuckets", "s3:PutObject", "s3:GetObject", "s3:PutObjectAcl" ], "Resource": [ "arn:aws:s3:::only4laughbucket" ] } ] } – Amit Shakya Feb 20 '19 at 15:09
  • Is this policy attached to the IAM user you are using? – Deiv Feb 20 '19 at 15:48
  • @Deiv yes this is my IAM user custom policy. – Amit Shakya Feb 20 '19 at 16:58
  • See here: https://stackoverflow.com/questions/36272286/getting-access-denied-when-calling-the-putobject-operation-with-bucket-level-per – Deiv Feb 20 '19 at 17:26

1 Answers1

0

I got know My Mistake, I set these all options to True, but I got know when we set ACL:'public-access' and in our put request then request rejects with error: Access DENIED as per Docs Amazon S3 Blocking Public access, I just reset the setting to false and it works for me.

Amit Shakya
  • 962
  • 3
  • 16
  • 30