Home > Blockchain >  Check if S3 bucket name begins with word?
Check if S3 bucket name begins with word?

Time:11-08

I'm trying to write a python Lambda to check for an S3 bucket in an account. The issue is that the naming of this bucket changes from account to account, with the same phrase in the beginning but randomized letters at the ending.

example:

bucketname-bucketregion-jdkaieldjadksl

with the name of the bucket, which is always the same, the region of the bucket, which may differ, and random letters, which always differ.

I know that you can check to see if a bucket exists using doesBucketExist, but is there an easy way to do this to check if a bucket begins with something?

The way I was thinking of doing this was getting all the buckets, then iterating through them to see if any name contained the bucketname but I'm worried this isn't a good approach.

CodePudding user response:

You can list the bucket names and check if the name prefix matches.

s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
  if bucket.name.startswith('bucketname-bucketregion-'):
      print(bucket.name)
  • Related