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.\-_]
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.\-_]
Share
Improve this question
edited Mar 21 at 20:31
John Rotenstein
271k28 gold badges447 silver badges531 bronze badges
Recognized by AWS Collective
asked Mar 21 at 12:33
Jim MacaulayJim Macaulay
5,1895 gold badges32 silver badges59 bronze badges
3
|
2 Answers
Reset to default 2It 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)
the error message "Bucket name must match the regex '^[a-zA-Z0-9.\-_]'"
is indicating that your bucket name, "Bucket_Source"
, contains an underscore (_
), which is not allowed in an S3 bucket name
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744354076a4570143.html
Prefix
parameter:for archive in src.objects.filter(Prefix='FirstLevelS3/SecondLevelS3/'): ...
– aneroid Commented Mar 21 at 15:20