具有公共访问权限将对象保存在AWS S3存储桶中

时间:2019-02-20 13:26:10

标签: amazon-web-services amazon-s3 aws-sdk-js aws-sdk-nodejs

每次在S3存储桶中创建一个具有动态名称的文件夹以及具有私密访问权限的多个图像时,一个图像,我需要授予公共访问权限,我在这里使用aws-sdk Javascript保存我的代码图片我需要授予公共访问权限

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

1 个答案:

答案 0 :(得分:0)

我知道我的错误,我将所有这些选项都设置为True,但是我知道当我们设置ACL:'public-access',并且在我们的put请求中然后请求拒绝并出现错误:根据Docs {{3 }},我只是将设置重置为false即可使用。