【发布时间】:2019-07-14 04:59:02
【问题描述】:
每次我在 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;
}
【问题讨论】:
-
谢谢,但我在保存公共读取文件时遇到错误“访问被拒绝”,可能是存储桶权限问题...
-
对不起,我没有找到那个解决方案,但这对我也不起作用,我觉得可能是我的 IAM 政策问题,这是我的 IAM 政策 { "Version": "2012-10-17 ", "声明": [ { "效果": "允许", "操作": [ "s3:ListAllMyBuckets", "s3:PutObject", "s3:GetObject", "s3:PutObjectAcl" ], "资源": [“arn:aws:s3:::only4laughbucket”]}]}
-
此策略是否附加到您正在使用的 IAM 用户?
-
@Deiv 是的,这是我的 IAM 用户自定义策略。
标签: amazon-web-services amazon-s3 aws-sdk-js aws-sdk-nodejs