forked from platform-engineering-org/creds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
50 lines (35 loc) · 1.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
import boto3
import gzip
import json
import os
import pathlib
import shutil
BUCKET_NAME = 'tf-creds-trail'
DOWNLOAD_PATH = 'AWSLogs/203972369401/CloudTrail/eu-west-2/2022/12/13/'
def download_items(bucket_name: str, download_path: pathlib.Path) -> None:
"""Prints to console the content of all json files are fount at download_path
Args:
bucket_name (str): The name of the bucket
download_path (pathlib.Path): The path in the bucket we want to get the content of the json files from
Returns:
None
"""
s3 = boto3.resource('s3')
s3_client = boto3.client('s3')
bucket = s3.Bucket(bucket_name)
my_bucket = s3_client.list_objects(
Bucket=bucket_name, Marker=download_path)['Contents']
for s3_object in my_bucket:
object_key = s3_object['Key']
file_name = os.path.basename(object_key)
with open(file_name, '+wb') as f:
bucket.download_fileobj(object_key, f)
with gzip.open(file_name, "rb") as f:
data = json.loads(f.read().decode("ascii"))
print(data)
os.remove(file_name)
def main():
download_items(BUCKET_NAME, DOWNLOAD_PATH)
if __name__ == "__main__":
main()