AccessDenied for ListObjectsV2 操作 S3 存储桶

tbo*_*one 7 continuous-integration amazon-s3 gitlab-ci

在 GitlabCi 期间,我得到:“致命错误:调用 ListObjectsV2 操作时发生错误(AccessDenied):拒绝访问”

我的存储桶策略:

{
"Version": "2008-10-17",
"Statement": [
    {
        "Sid": "AllowPublicRead",
        "Effect": "Allow",
        "Principal": {
            "AWS": "*"
        },
        "Action": "s3:*",
        "Resource": "arn:aws:s3:::BUCKET-NAME/*"
    }
]
Run Code Online (Sandbox Code Playgroud)

}

在 gitlabCI 设置中:

  • AWS_ACCESS_KEY_ID:您的 AWS-ACCESS-KEY-ID
  • AWS_SECRET_ACCESS_KEY:Your-AWS-SECRET-ACCESS-KEY
  • S3_BUCKET_NAME:YOUR-S3-BUCKET-NAME
  • DISTRIBUTION_ID: CLOUDFRONT-DISTRIBUTION-ID

我的 .gitlab-ci.yml

image: docker:latest

stages:
  - build
  - deploy

build:
  stage: build
  image: node:8.11.3
script:
  - export API_URL="d144iew37xsh40.cloudfront.net"
  - npm install
  - npm run build
  - echo "BUILD SUCCESSFULLY"
artifacts:
   paths:
    - public/
expire_in: 20 mins
environment:
name: production
only:
   - master
deploy:
  stage: deploy
  image: python:3.5
dependencies:
   - build
script:
  - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
  - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
  - export S3_BUCKET_NAME=$S3_BUCKET_NAME
  - export DISTRIBUTION_ID=$DISTRIBUTION_ID
  - pip install awscli --upgrade --user
  - export PATH=~/.local/bin:$PATH
  - aws s3 sync --acl public-read --delete public $S3_BUCKET_NAME
  - aws cloudfront create-invalidation --distribution-id 
$DISTRIBUTION_ID --paths '/*'
  - echo "DEPLOYED SUCCESSFULLY"

environment:
   name: production
only:
  - master
Run Code Online (Sandbox Code Playgroud)

jan*_*oth 15

我不确定接受的答案实际上是否可以接受,因为它只是允许对存储桶进行所有操作。此外,Sid 具有误导性... ;-)

这篇 AWS 文章提到了aws s3 sync.

这是相应政策的样子:

{
"Version": "version_id",
"Statement": [
    {
        "Sid": "AllowBucketSync",
        "Effect": "Allow",
        "Action": [
            "s3:GetObject", 
            "s3:PutObject", 
            "s3:ListBucket"
        ],
        "Resource": [
            "arn:aws:s3:::BUCKET-NAME",
            "arn:aws:s3:::BUCKET-NAME/*"
        ]
    }
] }
Run Code Online (Sandbox Code Playgroud)

  • 这是不正确的。s3:ListBucket 是允许用户列出存储桶中对象的权限名称。ListObjectsV2 是列出存储桶中的对象的 API 调用的名称。 (3认同)

mrb*_*den 5

我最近遇到了这个问题。无论我做了什么,无论我提供了什么权限,我在运行时不断收到“调用 ListObjectsV2 操作时发生错误(AccessDenied):拒绝访问”aws s3 ls <bucket>

我忘记了在我的环境中配置了多个 aws 配置文件。aws 命令使用的是默认配置文件,该配置文件具有一组不同的访问密钥。我必须为命令指定 --profile 标志:

aws s3 ls <bucket> --profile <correct profile>

那奏效了。这是一个利基情况,但也许它会帮助某人。