Skip to content

Commit

Permalink
feat: add s3 scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
dannysteenman committed Aug 10, 2023
1 parent bce9d7c commit 38b25c7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ A Collection of Awesome Tools and Scripts for Cloud Engineers.
- **[list_file_older_than_number_of_days.py](s3/list_file_older_than_number_of_days.py)** - Allows you to list all files older than N numbers of days.
- **[search_bucket_and_delete.py](s3/search_bucket_and_delete.py)** - Searches for your chosen bucketname and then deletes all (versioned)objects in that S3 bucket before deleting the bucket itself.
- **[search_file_in_bucket.py](s3/search_file_in_bucket.py)** - Allows you to search file in S3 bucket.
- **[search_key_bucket.py](s3/search_key_bucket.py)** - Searches for a single keys/object in an S3 bucket and let's you know wether it exists or not.
- **[search_multiple_keys_bucket.py](s3/search_multiple_keys_bucket.py)** - Searches for multiple keys/objects in an S3 bucket and let's you know wether it exists or not.
- **[search_subdirectory.py](s3/search_subdirectory.py)** - Allows you to search subdirectory under nested folder structure.

### SSM scripts
Expand Down
29 changes: 29 additions & 0 deletions s3/search_key_bucket.py
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)
29 changes: 29 additions & 0 deletions s3/search_multiple_keys_bucket.py
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}")

0 comments on commit 38b25c7

Please sign in to comment.