-
Notifications
You must be signed in to change notification settings - Fork 25
/
app.py
executable file
·147 lines (120 loc) · 3.96 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
139
140
141
142
143
144
145
146
147
#! /usr/bin/python
#encoding=utf-8
import json
from flask import Flask
from flask import json
from flask import request
from flask import send_from_directory
from flask import Response
import requests
import xmlparser
import base64
import hashlib
app = Flask(__name__, static_url_path='')
def from_request(request, k):
if not request.json:
raise Exception("Invalid Request")
return str(request.json[k])
def req(url, method):
command = requests.get
if method == 'DELETE':
command = requests.delete
elif method == 'PUT':
command = requests.put
elif method == 'POST':
command = requests.post
r = command(url)
return r.status_code
@app.route("/createbucket", methods = ['POST'])
def create():
url = from_request(request, 'url')
statuscode = req(url, 'PUT')
if statuscode == 200:
resp = Response(response='Success', status=statuscode)
elif statuscode == 400:
resp = Response(response='Invalid Bucket Name', status=statuscode)
elif statuscode == 409:
resp = Response(response='Conflict: Bucket already Exists',
status=statuscode)
elif statuscode == 403:
resp = Response(response='Access Denied', status=statuscode)
else:
resp = Response(response='Unknown Error', status=500)
return resp
@app.route("/deletebucket", methods = ['DELETE'])
def delete():
url = from_request(request, 'url')
statuscode = req(url, 'DELETE')
if statuscode == 204:
resp = Response(status=200)
elif statuscode == 404:
resp = Response(response='Bucket Not Exist', status=statuscode)
elif statuscode == 403:
resp = Response(response='Access Denied', status=statuscode)
elif statuscode == 409:
resp = Response(response='Confilct: Object Still Exists',
status=statuscode)
else:
resp = Response(response='Unknown Error', status=500)
return resp
@app.route("/putcors", methods = ['PUT'])
def putcors():
corsurl = from_request(request, 'url')
s3auth = from_request(request, 's3auth')
date = from_request(request, 'date')
cors = '''
<CORSConfiguration>
<CORSRule>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedOrigin>*</AllowedOrigin>
<AllowedHeader>*</AllowedHeader>
<ExposeHeader>x-amz-acl</ExposeHeader>
<ExposeHeader>ETag</ExposeHeader>
</CORSRule>
</CORSConfiguration>'''
content_md5 = base64.b64encode(hashlib.md5(cors.encode('utf8')).digest())
headers = {
'Content-type':'text/xml',
'Content-MD5':content_md5,
'Authorization':s3auth,
'Date' : date
}
r = requests.put(corsurl, headers=headers, data=cors)
statuscode = r.status_code
if statuscode == 200:
resp = Response(status=statuscode)
elif statuscode == 403:
resp = Response(response='Access Denied', status=statuscode)
else:
resp = Response(response='Unknown Error', status=500)
return resp
@app.route("/getservice", methods = ['POST'])
def listbucketsurl():
url = from_request(request, 'url')
s3auth = from_request(request, 's3auth')
date = from_request(request, 'date')
headers = {'Authorization':s3auth, 'x-amz-date': date}
r = requests.get(url, headers=headers)
statuscode = r.status_code
if statuscode != 200:
if statuscode == 403:
resp = Response(response='Access Denied', status=statuscode)
else:
resp = Response(response='Unknown Error', status=500)
return resp
content = r.text
buckets = xmlparser.getListFromXml(content, 'Bucket')
resp = Response(response=json.dumps(buckets), status=statuscode)
resp.headers['Content-type'] = 'application/json; charset=UTF-8'
return resp
@app.route("/")
def root():
return app.send_static_file('buckets.html')
@app.route("/<path:path>")
def static_files(path):
return send_from_directory('static', path)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, threaded=True)