-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery_s3.py
28 lines (22 loc) · 932 Bytes
/
query_s3.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
import argparse
import subprocess
import json
parser = argparse.ArgumentParser(description = "Search all accessible Amazon S3 buckets for filename matching input string.")
parser.add_argument("search_string", metavar = "S", type = str,
help = "The string to search for matching file names.")
args = parser.parse_args()
cmd_get_buckets = "aws s3api list-buckets"
process = subprocess.Popen(cmd_get_buckets.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
response = json.loads(output.decode())
buckets = response["Buckets"]
for bucket in buckets:
name = bucket["Name"]
cmd_search_bucket = "aws s3 ls s3://" + name + " --recursive"
process = subprocess.Popen(cmd_search_bucket.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
bucket_response = output.decode()
br_lines = bucket_response.splitlines()
for line in br_lines:
if args.search_string in line:
print(name, line)