【发布时间】:2019-11-04 01:03:37
【问题描述】:
我在 AWS 上新创建的存储桶的权限方面遇到问题。
我在 AWS-S3 上创建了一个存储桶,以便存储用户上传的应用程序中的图片。有时我会进去,删除图像。
我面临的问题是亚马逊建议对您的存储桶设置一个阻止所有公共访问权限,因此当我尝试读取我的图片时出现访问被拒绝错误我的应用程序。
我在他们的文档中花了将近 2 个小时,我承认这让我有点困惑,如果有人可以帮助我解决问题,我会很高兴。
我想保持存储桶的私密性,并使用密钥和秘密 ID 在我的应用程序的每个请求上签名,以防止未经授权的访问。下面是我的代码,我不知道我做错了什么。
const awsSdk = require("aws-sdk");
const multerS3 = require("multer-s3-transform");
awsSdk.config.update({
secretAccessKey: config.AWS_SECRET_KEY,
accessKeyId: config.AWS_SECRET_ID,
region: config.AWS_REGION
});
const awsStorage = multer({
storage: multerS3({
s3: s3,
bucket: "bucketname",
acl: "public-read",
contentType: multerS3.AUTO_CONTENT_TYPE,
cacheControl: "max-age=31536000",
metadata: (req, file, cb) => {
cb(null, { fieldname: file.originalname });
},
key: (req, file, cb) => {
cb(null, new Date().toISOString() + "-" + file.originalname);
}
}),
fileFilter: (req, file, cb) => {
if (
file.mimetype === "image/jpg" ||
file.mimetype === "image/jpeg" ||
file.mimetype === "image/png"
) {
//adding the file to the body custom field
req.body.files = req.files[0].fieldname;
cb(null, true); // accept file
} else {
cb(null, false); //reject file
}
}
存储桶策略 - 公开(如何制作它以便我可以使用我的密钥和 id 唱歌)
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::bucketname/*"
}
]
}
感谢您的帮助
【问题讨论】:
-
您应该使用预签名的 URL。见:Upload files to Amazon S3 from the browser using pre-signed urls
-
您没有包含任何实际使用
awsStorage的代码,也没有向我们展示您的客户端应用程序如何与此代码交互。
标签: amazon-web-services amazon-s3 aws-sdk