如何創建具有公共讀取訪問權限的存儲桶? (How to create a bucket with Public Read Access?)


問題描述

如何創建具有公共讀取訪問權限的存儲桶? (How to create a bucket with Public Read Access?)

我想對我的 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 界面中手動設置每個文件的公共讀取權限。

我做錯了什麼?


參考解法

方法 1:

Instead of using CorsConfiguration on the bucket, you need to attach a bucket policy to it. Try the following:

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: "*"

方法 2:

Accepted answer didn't work for me. CloudFormation failed to update the resource with the error:

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)

It appears the wildcard was missing in the resource definition. Full snippet that worked for me:

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: '*'

方法 3:

As the others have said, you need to implement a Bucket Policy such as this one:

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

This can be done in the AWS console by selecting the Bucket, then Permissions, then Bucket Policy. Looks like @Milan C. is indicating how to declare this in a serverless.yml file.

(by KlaasMilan CermakMax IvanovCSSian)

參考文件

  1. How to create a bucket with Public Read Access? (CC BY‑SA 2.5/3.0/4.0)

#serverless #amazon-s3






相關問題

計劃的 Lambda 函數無法進行第 3 方 API 調用 (Scheduled Lambda function not able to make 3rd party API calls)

如何創建具有公共讀取訪問權限的存儲桶? (How to create a bucket with Public Read Access?)

從代理後面登錄的無服務器框架? (Serverless Framework Login From Behind a Proxy?)

使用 AWS Lambda 對 Hasura 身份驗證掛鉤的空響應 (Empty response on Hasura auth hook using AWS Lambda)

從另一個 SAM 本地函數調用 AWS SAM 本地函數 (Invoke AWS SAM local function from another SAM local function)

package.json 文件在錯誤的文件夾中創建 (package.json file created in the wrong folder)

混合 Terraform 和無服務器框架 (Mixing Terraform and Serverless Framework)

將基於本地的每週商店時間轉換為 GMT 每週時間 (convert local based weekly store timings to GMT weekly timing)

從 AWS Lambda 函數創建 CloudFormation 堆棧,傳遞 API Gateway 參數 (Create CloudFormation stack from AWS Lambda function, passing API Gateway parameters)

減少我的 Express 應用程序的代碼大小 (Reduce code size of my Express application)

AWS Lambda 函數全局變量 (AWS Lambda function global variables)

我可以備份無服務器 SQL 池數據庫嗎? (Can I backup Serverless SQL pool Database?)







留言討論