Aws cli s3 An error occurred accessdenied when calling the listbuckets operation access denied

最後更新: 2022-05-13

目錄

  • list (ls)
  • upload & download (cp)
  • delete (rm)
  • Sync Content(sync)
  • Improve the transfer performance

S3 CLI

List

# list bucket

aws s3 ls

2021-08-19 03:21:44 my-import-export-bucket

P.S.

An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied

# list content in bucket

aws s3 ls s3://my-import-export-bucket

2021-08-19 03:22:55 1063377408 c7.mini.ova

P.S.

fatal error: An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied

Opts

  • --human-readable      # Unit: Bytes
  • --summarize              # CLI 尾列出 "Total Objects" & "Total Size"

Count files in bucket

aws s3 ls --recursive --human-readable --summarize s3://mybucket/

aws s3 ls --recursive | wc -l s3://mybucket/

P.S.

GUI: Amazon S3 > Buckets > YOUR_Bucket > Metircs     # 每隔一段時間採樣一次

Upload & Download

# Download File

aws s3 cp s3://mybucket/myfolder/file /backup

Completed 826.2 MiB/25.6 GiB (56.8 MiB/s) with 1 file(s) remaining

 * overwrite 時係沒有 confime 的

# Upload File

aws s3 cp test.txt s3://mybucket/myfolder

 * overwrite 時係沒有 confime 的

# Upload Folder

aws s3 cp myfolder --recursive s3://mybucket/myfolder

Delete

aws s3 rm s3://mybucket/myfolder/file

Doc

http://docs.aws.amazon.com/cli/latest/reference/s3/index.html


Sync Content

Recursively copies new(新建立) or updated(時間不同, 並不是指新過) files from the source directory to the destination (no delete on dest)

Only creates folders in the destination if they contain one or more files.

Opt:

  • --dryrun

# Recursive Permission

# Command is performed on all files or objects under the specified directory or prefix.

# Only creates folders in the destination if they contain one or more files.

# To run the command aws s3 cp with the --recursive option, you need permission to

s3:GetObject, s3:PutObject, s3:DeleteObject, and s3:ListBucket

P.S.

An error occurred (AccessDenied) when missing "ListObjectsV2" permission

Note:

s3:ListBucket is the name of the permission that allows a user to list the objects in a bucket.

ListObjectsV2 is the name of the API call that lists the objects in a bucket.

"Action": [ "s3:ListBucket", ], "Resource": [ "arn:aws:s3:::bucketname", "arn:aws:s3:::bucketname/*" ]

* Specify bucket resource ARN for the ListBucketVersions and 2 more actions.

ie.

# Local -> S3

aws s3 sync . s3://mybucket

# S3 -> Local

aws s3 sync s3://mybucket ./

download: s3://mybucket/test1.txt to ./test1.txt download: s3://mybucket/test3.txt to ./test3.txt download: s3://mybucket/test2.txt to ./test2.txt

 * updated(時間不同, 並不是指新過) files

touch test2.txt

aws s3 sync s3://mybucket ./

download: s3://mybucket/test2.txt to ./test2.txt

# Delete dest missing

# any files existing under the specified prefix and bucket

but not existing in the local directory will be deleted.

aws s3 sync . s3://mybucket --delete

# Exclude file

aws s3 sync ./myfolder s3://mybucket/myfolder --exclude *.tmp

# S3 to S3

aws s3 sync s3://mybucket s3://mybucket2

aws s3 sync s3://mybucket/myfolder s3://mybucket/myfolder2

# two buckets in different regions

aws s3 sync s3://my-us-west-2-bucket s3://my-us-east-1-bucket --source-region us-west-2 --region us-east-1

# Verify that the objects are copied

aws s3 ls --recursive --summarize s3://mybucket-SOURCE > bucket-contents-source.txt

 * Versioning 的檔案不 count 在內 !!

aws s3 ls --recursive --summarize s3://mybucket-TARGET > bucket-contents-target.txt


Improve the transfer performance

max_concurrent_requests

At any given time, multiple requests to Amazon S3 are in flight. (Default: 10)

config

aws configure set default.s3.max_concurrent_requests 15

cat ~/.aws/config

[default] region = ap-east-1 s3 = max_concurrent_requests = 15

Why is my S3 bucket Access Denied?

If you're getting Access Denied errors on public read requests that are allowed, check the bucket's Amazon S3 Block Public Access settings. Review the S3 Block Public Access settings at both the account and bucket level. These settings can override permissions that allow public read access.

How do I fix an AWS S3 bucket policy and Public permissions access denied error?

To resolve these issues:.
Check that the IAM user or role has s3:Get:BucketPolicy permission to view the bucket policy and s3:PutBucketPolicy permission to edit it. ... .
If you're denied permissions, then use another IAM identity that has bucket access, and edit the bucket policy..

When calls the ListObjectsV2 Operation Access Denied AWS?

To solve the "(AccessDenied) when calling the ListObjectsV2 operation" error attach a policy that allows the ListBucket action on the bucket itself and the GetObject action on all of the bucket's objects to the IAM entity (user or role) that is trying to access the S3 bucket.

Why am I getting an access denied error for ListObjectsV2 when I run the sync command on my Amazon S3 bucket?

If your bucket belongs to another AWS account and has Requester Pays enabled, verify that your bucket policy and IAM permissions both grant ListObjectsV2 permissions. If the ListObjectsV2 permissions are properly granted, then check your sync command syntax.