-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (54 loc) · 2.15 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
import argparse
import os
# -----------------FUNCTIONS----------------------------------------------------
def getOriginalFilename(resHeaders,presignedURL):
""" Gets the original filename. Tries to do so using the response
headers. In case of issues, takes the filename from the provided
pre-signed URL.
"""
try:
return resHeaders["X-Amz-Meta-Orig-Filename"]
except:
return os.path.basename(presignedURL.split('?')[0])
def getFile(presignedURL, destination):
""" Pulls an object from the bucket given its pre-signed URL and places it
in a given destination.
"""
# Make request
res = requests.get(presignedURL)
# Check response code
if res.status_code != 200:
return False
# Get filename
filename = getOriginalFilename(res.headers,presignedURL)
# Save received file
with open("%s/%s" % (destination,filename), 'wb') as fd:
for chunk in res.iter_content(chunk_size=128):
fd.write(chunk)
# -----------------CMD OPTIONS--------------------------------------------------
parser = argparse.ArgumentParser(description='Test-Suite results fetch.')
parser.add_argument('-p','--pre-signed-URLs',
help='Path to file listing pre-signed URLs.',
type=str,
dest="pathPresignedURLs",
required=True)
parser.add_argument('-d','--destination',
help='Location where to save fetched files.',
type=str,
dest="destination",
default=".")
args = parser.parse_args()
# For each URL on the list, get a file
for presignedURL in open(args.pathPresignedURLs).readlines():
presignedURL = presignedURL.strip()
print("Getting file from: %s..." % presignedURL[:100])
try:
if getFile(presignedURL,args.destination) == False:
print(" ERROR: unable to get file from: %s..." % presignedURL[:100])
else:
print(" OK")
except BaseException as ex:
print(" ERROR: unable to get file from: %s..." % presignedURL[:100])
print(ex)
print(" -------- ")