-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bce9d7c
commit 38b25c7
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# https://github.com/dannysteenman/aws-toolbox | ||
# | ||
# License: MIT | ||
# | ||
# This script searches for a single keys/object in an S3 bucket and let's you know wether it exists or not | ||
# | ||
# Usage: python search_key_bucket.py | ||
|
||
import boto3 | ||
import botocore | ||
|
||
|
||
def key_exists(bucket, key): | ||
s3 = boto3.client("s3") | ||
try: | ||
s3.head_object(Bucket=bucket, Key=key) | ||
print(f"Key: '{key}' found!") | ||
except botocore.exceptions.ClientError as e: | ||
if e.response["Error"]["Code"] == "404": | ||
print(f"Key: '{key}' does not exist!") | ||
else: | ||
print("Something else went wrong") | ||
raise | ||
|
||
|
||
bucket = "my-bucket" | ||
key = "path/to/my-file.txt" | ||
|
||
key_exists(bucket, key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# https://github.com/dannysteenman/aws-toolbox | ||
# | ||
# License: MIT | ||
# | ||
# This script searches for multiple keys/objects in an S3 bucket and let's you know wether it exists or not | ||
# | ||
# Usage: python search_multiple_keys_bucket.py | ||
|
||
import boto3 | ||
|
||
|
||
def check_keys_exist(bucket, keys_to_check): | ||
s3 = boto3.client("s3") | ||
response = s3.list_objects_v2(Bucket=bucket) | ||
|
||
if "Contents" in response: | ||
existing_keys = {item["Key"] for item in response["Contents"]} | ||
return {key: key in existing_keys for key in keys_to_check} | ||
else: | ||
return {key: False for key in keys_to_check} | ||
|
||
|
||
bucket = "my-bucket" | ||
keys_to_check = ["path/to/file1.txt", "path/to/file2.txt", "path/to/file3.txt"] | ||
|
||
result = check_keys_exist(bucket, keys_to_check) | ||
|
||
for key, exists in result.items(): | ||
print(f"Key {key} exists: {exists}") |