首页 > 解决方案 > AWS S3 上传失败,ACL public-true

问题描述

我在 Ruby 中有这个简单的脚本来上传我从 AWS 文档中获得的 S3 上的文件:

def object_uploaded?(s3_resource, bucket_name, object_key, file_path)
  object = s3_resource.bucket(bucket_name).object(object_key)
  File.open(file_path, 'rb') do |file|
    object.put(body: file, acl: 'public-read')
  end
  return true
rescue StandardError => e
  puts "Error uploading object: #{e.message}"
  return false
end 

当我添加acl: 'public-read'时,该脚本会拒绝访问。如果我删除它,效果很好。

唯一可行的方法是公开我的 S3 存储桶。 在此处输入图像描述

这是我的存储桶策略:

{
    "Version": "2012-10-17",
    "Id": "Policy1610635552932",
    "Statement": [
        {
            "Sid": "Stmt1610635551842",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::[mybucket]/*"
        }
    ]
}

这是我的 IAM 政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:ListStorageLensConfigurations",
                "s3:GetAccessPoint",
                "s3:PutAccountPublicAccessBlock",
                "s3:GetAccountPublicAccessBlock",
                "s3:ListAllMyBuckets",
                "s3:ListAccessPoints",
                "s3:ListJobs",
                "s3:PutStorageLensConfiguration",
                "s3:CreateJob"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::[mybucket]",
                "arn:aws:s3:::[mybucket]/*"
            ]
        }
    ]
}

有什么方法可以按照 AWS 的建议打开阻止公共访问并通过设置 acl 来获得公共可访问的文件?我的政策有什么问题?

标签: amazon-web-servicesamazon-s3

解决方案


如果存储桶的阻止公共访问设置已打开,您将无法上传具有 ACL 公开读取的文件。根据文档:“Amazon S3 阻止公共访问阻止应用任何允许对 S3 存储桶中的数据进行公共访问的设置。” (https://docs.aws.amazon.com/AmazonS3/latest/user-guide/block-public-access.html


推荐阅读