-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_generate_keys.py
106 lines (84 loc) · 2.9 KB
/
02_generate_keys.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import argparse
import json
import os
import boto3
def list_s3_keys(bucket_name, deployment_id=""):
"""
List all keys in an S3 bucket under a specific prefix.
Parameters:
bucket_name (str): The name of the S3 bucket.
prefix (str): The prefix to filter keys (default: "").
Returns:
list: A list of S3 object keys.
"""
with open("./credentials.json", encoding="utf-8") as config_file:
aws_credentials = json.load(config_file)
session = boto3.Session(
aws_access_key_id=aws_credentials["AWS_ACCESS_KEY_ID"],
aws_secret_access_key=aws_credentials["AWS_SECRET_ACCESS_KEY"],
region_name=aws_credentials["AWS_REGION"],
)
s3_client = session.client("s3", endpoint_url=aws_credentials["AWS_URL_ENDPOINT"])
keys = []
continuation_token = None
while True:
list_kwargs = {
"Bucket": bucket_name,
"Prefix": deployment_id,
}
if continuation_token:
list_kwargs["ContinuationToken"] = continuation_token
response = s3_client.list_objects_v2(**list_kwargs)
# Add object keys to the list
for obj in response.get("Contents", []):
keys.append(obj["Key"])
# Check if there are more objects to list
if response.get("IsTruncated"): # If True, there are more results
continuation_token = response["NextContinuationToken"]
else:
break
return keys
def save_keys_to_file(keys, output_file):
"""
Save S3 keys to a file, one per line.
Parameters:
keys (list): List of S3 keys.
output_file (str): Path to the output file.
"""
with open(output_file, "w") as f:
for key in keys:
f.write(key + "\n")
def main():
parser = argparse.ArgumentParser(
description="Generate a file containing S3 keys from a bucket."
)
parser.add_argument(
"--bucket", type=str, required=True, help="Name of the S3 bucket."
)
parser.add_argument(
"--deployment_id",
type=str,
default="",
help="The deployment id to filter objects. If set to '' then all deployments are used. (default: '')",
)
parser.add_argument(
"--output_file",
type=str,
default="s3_keys.txt",
help="Output file to save S3 keys.",
)
args = parser.parse_args()
# List keys from the specified S3 bucket and prefix
print(
f"Listing keys from bucket '{args.bucket}' with deployment '{args.deployment_id}'..."
)
keys = list_s3_keys(args.bucket, args.deployment_id)
# create dir if not existant
dirname = os.path.dirname(args.output_file)
if not os.path.exists(dirname):
os.makedirs(dirname)
# Save keys to the output file
save_keys_to_file(keys, args.output_file)
print(f"Saved {len(keys)} keys to {args.output_file}")
if __name__ == "__main__":
main()