Get all s3 buckets with a prefix

Currently we have several buckets with the application prefix and the domain suffix for example Bucket Names

  • myapp-us-east-1
  • Myapp-us-west-1

Is there a way to find all buckets with a specific prefix? There is something like:

s3 = boto3.resource('s3')
buckets = s3.buckets.filter(Prefix="myapp-")
+5
source share
2 answers

The high-level collection s3.buckets.filter(Filters=somefilter)only works for the methods described in the Filters describe_tags(list) section . In this case, you MUST mark your container ( s3.BucketTagging) before you can use a very specific filtering method s3.buckets.filter(Filters=formatted_tag_filter) ( http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#EC2.Client )

IMHO, , - AWS.

s3 = boto3.resource('s3')
for bucket in s3.buckets.all(): 
    if bucket.name.startswith("myapp-"):
        print bucket.name

, ( ) (http://boto3.readthedocs.org/en/latest/guide/collections.html)

# S3 list all keys with the prefix '/photos'
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
    if bucket.name.startswith("myapp-") :
        for obj in bucket.objects.filter(Prefix='/photos'):
            print('{0}:{1}'.format(bucket.name, obj.key))

:

ListBuckets, ListObjects HeadObject . S3, .

+11

All Articles