【问题标题】:Serverless with S3 bucket to serve assets使用 S3 存储桶为资产提供服务的无服务器
【发布时间】:2020-03-02 02:48:54
【问题描述】:

我使用 Bref (https://bref.sh/)。我尝试使用 AWS S3 配置 serverless.yml 文件,以存储 img、css、js 等资产。当我使用“无服务器部署”命令进行部署时,出现此错误:

发生错误:AssetsBucketPolicy - API: s3:PutBucketPolicy Access 拒绝。

在我的 AWS 账户中,我拥有“AdministratorAccess”权限 (https://www.youtube.com/watch?v=KngM5bfpttA&list=PL0_-jlAhLRgEcU0P0Ivi4OO844pgrzJOU&index=2&t=0s)

策略AdministratorAccess

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "*",
            "Resource": "*"
        }
    ]
}

我的 serverless.yml 文件是:

service: bref-demo-symfony

provider:
    name: aws
    region: us-east-1
    runtime: provided
    environment:
        # Symfony environment variables
        APP_ENV: prod

plugins:
    - ./vendor/bref/bref

functions:
    website:
        handler: public/index.php
        timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
        layers:
            - ${bref:layer.php-73-fpm}
        events:
            -   http: 'ANY /'
            -   http: 'ANY /{proxy+}'
    console:
        handler: bin/console
        timeout: 120 # in seconds
        layers:
            - ${bref:layer.php-73} # PHP
            - ${bref:layer.console} # The "console" layer

resources:
    Resources:
        # The S3 bucket that stores the assets
        Assets:
            Type: AWS::S3::Bucket
            Properties:
                BucketName: my-unique-serverless-assets-bucket
        # The policy that makes the bucket publicly readable
        AssetsBucketPolicy:
            Type: AWS::S3::BucketPolicy
            Properties:
                Bucket: !Ref Assets # References the bucket we defined above
                PolicyDocument:
                    Statement:
                        -   Effect: Allow
                            Principal: '*' # everyone
                            Action: 's3:GetObject' # to read
                            Resource: 'arn:aws:s3:::my-unique-serverless-assets-bucket/*' # things in the bucket

在 AWS S3 上,我尝试使用

在存储桶上添加策略
 {
  "Id": "Policy1573043469280",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1573043465451",
      "Action": "s3:*",
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::bref-demo-symfony-dev-serverless-assets/assets",
      "Principal": "*"
    }
  ]
}

我收到一条消息,例如“拒绝访问”、“您无法授予公共访问权限,因为阻止公共访问设置 已为此帐户启用。确定哪些设置是 打开,检查您的阻止公共访问设置。”为什么?

我不明白如何配置它?这个权限(AdministratorAccess)不够?

谢谢!

【问题讨论】:

标签: amazon-s3 amazon-iam serverless bref


【解决方案1】:

从文档中,您可以看到:

要解决“拒绝访问”错误,请检查以下内容:

您的 IAM 身份对 s3:GetBucketPolicys3:PutBucketPolicy.

https://aws.amazon.com/premiumsupport/knowledge-center/s3-access-denied-bucket-policy/

请检查您为 Lambda 函数配置的角色是否具有此权限。

您可以在“执行角色”部分看到这一点: 在这里你可以看到我的 Lambda 函数的角色是“claudia-express-executor”。

您也可以点击它,详细查看该角色权限是什么。

【讨论】:

  • 我有管理员访问权限 { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "", " Resource": "" } ] } 我也添加了 AmazonS3FullAccess { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3: *", "资源": "*" } ] }
  • 抱歉,你一直说有AdministratorAccess,Lambda有吗?检查我的更新
【解决方案2】:
  1. 检查存储桶策略
  2. 如果涉及存储桶策略,请添加您为无服务器创建的用户
  3. 查看下图以获取示例存储桶策略 https://docs.aws.amazon.com/AmazonS3/latest/user-guide/add-bucket-policy.html

【讨论】:

  • 我收到类似“拒绝访问”的消息,“在此策略中授予公共访问权限将被阻止,因为此帐户和存储桶启用了公共访问阻止设置。要识别启用的设置,请检查您的公共访问阻止设置。”
  • 进入您的存储桶 > 权限 > 公共访问设置 > 编辑 > 取消勾选阻止新公共 ACL 和上传公共对象并删除通过公共 ACL 授予的公共访问权限(警告)
【解决方案3】:

尝试添加iamRoleStatements,例如,如果您需要获取和放置对象,请将此代码添加到iamRoleStatements,例如:

provider:
  name: aws
  runtime: nodejs10.x
  region: us-west-2
  profile: ${self:custom.profiles.${self:custom.myStage}}
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "s3:PutObject"
        - "s3:GetObject"
      Resource:
        - "*"

这是另一个例子:

provider:
  name: aws
  iamRoleStatements:
    - Effect: 'Allow'
      Action:
        - 's3:ListBucket'
      Resource:
        Fn::Join:
          - ''
          - - 'arn:aws:s3:::'
            - Ref: ServerlessDeploymentBucket
    - Effect: 'Allow'
      Action:
        - 's3:PutObject'
      Resource:
        Fn::Join:
          - ''
          - - 'arn:aws:s3:::'
            - Ref: ServerlessDeploymentBucket
            - '/*'

如果您需要更多信息,请阅读无服务器文档: Serverless IAM Roles

【讨论】:

  • 对不起,我没有成功。谢谢你的帮助!
  • 尝试为您的存储桶添加公共访问权限
猜你喜欢
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 2021-01-25
  • 2021-12-31
  • 2019-05-05
  • 2013-06-25
  • 2014-05-28
相关资源
最近更新 更多