Skip to content

Instantly share code, notes, and snippets.

@MahmoudDolah
Last active February 6, 2020 19:52
Show Gist options
  • Save MahmoudDolah/d63a04684e4c7b36e36dc2f758387ff8 to your computer and use it in GitHub Desktop.
Save MahmoudDolah/d63a04684e4c7b36e36dc2f758387ff8 to your computer and use it in GitHub Desktop.
Checks every available S3 bucket for a given key
#! /usr/bin/env python3
import boto3
s3_client = boto3.client('s3')
s3 = boto3.resource('s3')
buckets_response = s3_client.list_buckets()
KEY = 'KEY_NAME'
for bucket_response in buckets_response['Buckets']:
bucket_name = bucket_response['Name']
bucket = s3.Bucket(bucket_name)
objs = list(bucket.objects.filter(Prefix=KEY))
if len(objs) > 0 and objs[0].key == KEY:
print("The key " + KEY + " exists in bucket " + bucket_name)
else:
# print("Not in " + bucket_name)
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment