【问题标题】:AWS S3 upload fails with ACL public-trueAWS S3 上传失败,ACL public-true
【发布时间】:2021-04-20 07:57:47
【问题描述】:

我在 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 来获得公共可访问的文件?我的政策有什么问题?

【问题讨论】:

  • 你能说明你想要达到什么目标吗?您的对象是公开的或私有的。使用 ACL 或存储桶策略将其公开并不重要 - 公开就是公开的。
  • S3 ACL 是一种早于 IAM 的传统访问控制机制。如果您已经使用它,则无需更改,但我建议您在新项目中尽量避免使用它。
  • 我想要实现的是上传文件并使其可公开访问。我想我可以通过设置 ACL 而不更改存储桶设置来做到这一点。但显然,如果存储桶 Block public access 开启,我什至无法上传带有 ACL public-read 的文件。

标签: amazon-web-services amazon-s3


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-04-12
    • 2014-11-15
    • 1970-01-01
    • 2018-12-31
    • 2017-08-16
    • 1970-01-01
    • 2015-01-21
    • 2019-12-01
    • 2015-11-07
    相关资源
    最近更新 更多