-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1-get-bucket-contents.py
executable file
·58 lines (40 loc) · 1.57 KB
/
1-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
#!/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 json
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)