简体   繁体   中英

List content of subfolder in AWS S3 Bucket

I'm trying to list files in a specific folder in an S3 bucket. I only have the permission to access this specific folder, and not to the rest of the bucket. My problem is that I cannot list the content of the folder. Every time I try, I get the message "Message: An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied".

The commands I'm using are:

s3_client = boto3.client('s3') 
all_objects = s3_client.list_objects_v2(Bucket="my_bucket", Prefix="name_of_the_folder")

I know I have access to the folder because if I know the exact name of a file that is in the folder, I can access it using:

s3_client = boto3.client('s3')
my_object= s3_client.get_object(Bucket="my_bucket", Key="name_of_the_folder/name_of_the_file"

The policy of the principal accessing the Bucket is the following:

PolicyName: Bucket-Access
PolicyDocument:
    Version: '2012-10-17'
    Statement:
        - Effect: Allow
          Action:
             - s3:GetObject
             - s3:PutObject
             - s3:ListBucket
             Resource: [arn:aws:s3:::my-bucket/folder-i-have-access-to/*]

You need to have access on the folder itself and it contents. The policy has to be like this:

PolicyName: Bucket-Access
PolicyDocument:
    Version: '2012-10-17'
    Statement:
        - Effect: Allow
          Action:
             - s3:GetObject
             - s3:PutObject
             - s3:ListBucket
             Resource: 
                 - arn:aws:s3:::my-bucket/folder-i-have-access-to/*
                 - arn:aws:s3:::my-bucket/folder-i-have-access-to/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM