forked from dmuth/s3-disk-usage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_get_bucket_contents.py
executable file
·60 lines (47 loc) · 1.59 KB
/
a_get_bucket_contents.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
#!/usr/bin/env python3
"""This script is used to extract the contents of an S3 bucket, including
verison info and files that have been deleted.
Note that we're using subprocess, as it appears to be a replacement
for os.system() and other functions, as per https://stackoverflow.com/a/4813571/196073"""
import argparse
import logging
import os
import subprocess
import sys
import tempfile
logging.basicConfig(
level=logging.INFO, format="%(asctime)s: %(levelname)s: %(message)s"
)
LOGGER = logging.getLogger()
#
# Parse our arguments.
#
PARSER = argparse.ArgumentParser(
description="Extract all versions of files in an S3 bucket"
)
PARSER.add_argument("bucket")
PARSER.add_argument(
"file",
nargs="?",
help="JSON file to write (default: output.json)",
default="output.json",
)
# PARSER.add_argument("--filter", help = "Filename text to filter on")
ARGS = PARSER.parse_args()
LOGGER.info("Args: %s", ARGS)
OUTPUT = ARGS.file
BUCKET = ARGS.bucket
CMD = "aws s3api list-object-versions --bucket %s" % BUCKET
# CMD = "ls what" # Debugging
TMP_FD, TMPFILE = tempfile.mkstemp(dir=".", prefix="tmp-output")
LOGGER.info("Temp file '%s' created!", TMPFILE)
LOGGER.info("Executing command '%s'", CMD)
LOGGER.info("Note that this may take a long time, perhaps a minute or more!")
COMPLETED = subprocess.run(CMD, stdout=TMP_FD, shell=True)
if COMPLETED.returncode:
LOGGER.error(
"! Process '%s' exited with return code '%d'", CMD, COMPLETED.returncode
)
sys.exit(COMPLETED.returncode)
LOGGER.info("Renaming temp file '%s' to '%s'...", TMPFILE, OUTPUT)
os.rename(TMPFILE, OUTPUT)