I am trying to use the AWS s3 commands to list and get objects from the terminal. I already configurated the credentials with the private key of the IAM user, and I added the policy in the bucket as well, but it does not matter what I do, I always get this error:
aws s3 ls s3://folder-staging/file01.txt --profile folder-staging
"An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied
"
This is my policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowGetObjects",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::233420108572:user/manolo"
},
"Action": [
"s3:GetObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::folder-staging",
"arn:aws:s3:::folder-staging/*"
]
}
]
}
I also trying a more permissive variation but it does not work either:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowGetObjects",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": [
"s3:GetObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::folder-staging",
"arn:aws:s3:::folder-staging/*"
]
}
]
}
I also swtiched "Block public access (bucket settings)" to Off
2
Answers
First and most important. Go and put back the "Block public access" flag, unless you want your bucket to be publicly accessible, which judging from your bucket policy i think is not your case. (https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-control-block-public-access.html)
From what you describe i suppose you want to access your bucket from the user
arn:aws:iam::233420108572:user/manolo
.So there is a couple of things to check for throubleshooting:
s3 ls
with the correct credentials. To do so you can use the AWS CLI, just run:and see if you are actually acting as the user
arn:aws:iam::233420108572:user/manolo
.Let me know if this helps you. I will also leave here a blogpost with some throubleshooting for your error: https://repost.aws/knowledge-center/s3-troubleshoot-403
If you wish to grant access to a specific user, it is typically better to grant access via a policy on the IAM User rather than via a Bucket Policy. Bucket Policies are typically only used to grant ‘public’ or cross-account access.
It would be similar to the policy you already wrote: