【问题标题】:How to create a bucket with Public Read Access?如何创建具有公共读取访问权限的存储桶?
【发布时间】:2019-06-13 01:05:19
【问题描述】:

我想对我的 Bucket 中 serverless.yml 文件的“public”文件夹中的所有项目启用公共读取访问权限。

目前这是我用来声明我的存储桶的定义代码。它是从一个无服务器堆栈示例中复制和粘贴的。

Resources:
  AttachmentsBucket:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: PublicRead
      # Set the CORS policy
      BucketName: range-picker-bucket-${self:custom.stage}
      CorsConfiguration:
        CorsRules:
          -
            AllowedOrigins:
              - '*'
            AllowedHeaders:
              - '*'
            AllowedMethods:
              - GET
              - PUT
              - POST
              - DELETE
              - HEAD
            MaxAge: 3000

# Print out the name of the bucket that is created
Outputs:
  AttachmentsBucketName:
    Value:
      Ref: AttachmentsBucket

现在,当我尝试使用文件的 url 时,它返回访问被拒绝。我必须在 aws-s3 Web 界面中手动设置每个文件的公共读取权限。

我做错了什么?

【问题讨论】:

    标签: amazon-s3 serverless


    【解决方案1】:

    接受的答案对我不起作用。 CloudFormation 未能更新资源并出现以下错误:

    Action does not apply to any resource(s) in statement (Service: Amazon S3; Status Code: 400; Error Code: MalformedPolicy; Request ID: <...>; S3 Extended Request ID: <...>; Proxy: null)

    资源定义中似乎缺少通配符。对我有用的完整 sn-p:

    PublicBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: 'public-bucket-name'
    
    PublicBucketPolicy:
      Type: AWS::S3::BucketPolicy
      Properties:
        Bucket: !Ref PublicBucket
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - 's3:GetObject'
              Resource:
                - !Join ['/', [!GetAtt [PublicBucket, Arn], '*']]
              Principal: '*'
    

    【讨论】:

      【解决方案2】:

      正如其他人所说,您需要实施 Bucket Policy,例如:

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

      这可以在 AWS 控制台中完成,方法是依次选择 Bucket、Permissions、Bucket Policy。看起来@Milan C. 正在指示如何在 serverless.yml 文件中声明它。

      【讨论】:

        【解决方案3】:

        您需要在存储桶上使用attach a bucket policy,而不是使用CorsConfiguration。请尝试以下操作:

        Resources:
          AttachmentsBucket:
            Type: AWS::S3::Bucket
            Properties:
              BucketName: range-picker-bucket-${self:custom.stage}
        
          AttachmentsBucketAllowPublicReadPolicy:
            Type: AWS::S3::BucketPolicy
            Properties:
              Bucket: !Ref AttachmentsBucket
              PolicyDocument:
                Version: "2012-10-17"
                Statement: 
                  - Effect: Allow
                    Action: 
                      - "s3:GetObject"
                    Resource: 
                      - !Join ['/', [!Ref AttachmentsBucket, 'public']]
                    Principal: "*"
        
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-23
        • 1970-01-01
        • 2019-07-14
        • 2019-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多