pythonamazon-web-servicesamazon-s3boto3

Sub Folders of S3 bucket - Bucket name must match the regex "^[a-zA-Z0-9.\-_]


I have below code to get the list of objects in the bucket, but it does not work for the sub folders of the S3.

import boto3

bucket_from = "BucketSource"
s3 = boto3.resource('s3')
src = s3.Bucket(bucket_from)

for archive in src.objects.all():
    print(archive.key)

While using sub folders, FirstLevelS3/SecondLevelS3/BucketSource, this is the error:

Bucket name must match the regex "^[a-zA-Z0-9.\-_]

Solution

  • It sounds like you are trying to list S3 objects under a given key prefix and to do that you have accidentally provided the key prefix as a suffix on the bucket name, for example:

    src = s3.Bucket('mybucket/myphotos/dogs/')
    
    for obj in src.objects.all():
        print(obj.key)
    

    That fails because you have supplied a bucket name of mybucket/myphotos/dogs/ which indeed is not legal for a bucket name.

    You should instead only use the bucket name e.g. mybucket when calling s3.Bucket(...) and then filter the objects on the desired key prefix, for example:

    src = s3.Bucket('mybucket')
    
    for obj in src.objects.filter(Prefix='myphotos/dogs/'):
        print(obj.key)