【问题标题】:AWS-S3 Bucket reading permissions issueAWS-S3 存储桶读取权限问题
【发布时间】: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/*"
    }
  ]
}

感谢您的帮助

【问题讨论】:

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


【解决方案1】:

我看到的第一件事是public-read acl。你很可能不想这样做。您可以在此处阅读该主题: https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html

除此之外,John Rotenstein 应该可以帮助您完成剩下的工作。

【讨论】:

    猜你喜欢
    • 2022-01-03
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 2012-02-22
    • 2019-01-23
    • 2018-09-11
    相关资源
    最近更新 更多