-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
138 lines (105 loc) · 4.09 KB
/
app.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from flask import Flask, render_template, url_for, request, redirect
import json
import boto3
import botocore
app = Flask(__name__)
with open('config.json', 'r') as f:
config = json.loads(f.read().strip())
endpoint = config['endpoint']
accessKey = config['accessKey']
secretAccessKey = config['secretAccessKey']
bucketName = config['bucketName']
region = config['region']
presignedURLExpiration = config['presignedURLExpiration']
def s3ClientCall():
try:
s3 = boto3.client('s3',
aws_access_key_id = accessKey,
aws_secret_access_key = secretAccessKey,
endpoint_url = endpoint,
region_name = region
)
return(s3)
except ValueError as e:
return(str(e))
@app.route('/', methods=['POST', 'GET'])
def index():
clientCall = s3ClientCall()
try:
bucketObjects = clientCall.list_objects(
Bucket = bucketName,
Delimiter = '/',
)
print()
print("[#] All directories/objects in main dir: ")
print(bucketObjects)
print()
objects = []
dirs = []
if 'ResponseMetadata' in bucketObjects and bucketObjects['ResponseMetadata']['HTTPStatusCode'] == 200:
if "Contents" in bucketObjects:
for files in bucketObjects['Contents']:
bucketKeys = files['Key']
print(bucketKeys)
objects.append(bucketKeys)
if "CommonPrefixes" in bucketObjects:
for directories in bucketObjects['CommonPrefixes']:
bucketDirs = directories['Prefix']
print(bucketDirs)
dirs.append(bucketDirs)
else:
print("[!] There was some issue fetching the contents of the bucket")
print(request.method, request)
# return bucketKeys, bucketDirs
return render_template('index.html', files=objects, directories=dirs)
except botocore.exceptions.ParamValidationError as e:
return render_template('error.html', exception=e)
except botocore.exceptions.ClientError as e:
return render_template('error.html', exception=e)
except AttributeError:
return render_template('error.html', exception=clientCall)
@app.route('/list', methods=['GET'])
def list():
clientCall = s3ClientCall()
directory = request.args.get('directory')
bucketObjects = clientCall.list_objects(
Bucket = bucketName,
Delimiter = '/',
Prefix = directory,
)
objects = []
dirs = []
if 'ResponseMetadata' in bucketObjects and bucketObjects['ResponseMetadata']['HTTPStatusCode'] == 200:
if "Contents" in bucketObjects:
for files in bucketObjects['Contents']:
bucketKeys = files['Key']
print(bucketKeys)
objects.append(bucketKeys)
if "CommonPrefixes" in bucketObjects:
for directories in bucketObjects['CommonPrefixes']:
bucketDirs = directories['Prefix']
print(bucketDirs)
dirs.append(bucketDirs)
else:
print("[!] There was some issue fetching the contents of the bucket")
print(request.method, request)
# return bucketKeys, bucketDirs
return render_template('index.html', files=objects, directories=dirs)
@app.route('/play', methods=['GET'])
def play():
clientCall = s3ClientCall()
objectName = request.args.get('name')
print(objectName, type(objectName))
presignedURL = clientCall.generate_presigned_url('get_object',
Params = {
'Bucket': bucketName,
'Key': objectName
},
ExpiresIn = presignedURLExpiration
)
print()
print(f"[#] Presigned URL: {presignedURL}")
print()
return render_template('play.html', videoURL=presignedURL, objectName=objectName)
if __name__ == '__main__':
app.run(debug = True)