-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathapplication.py
76 lines (59 loc) · 2.37 KB
/
application.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
####
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
####
from flask import Flask, render_template, request, redirect, url_for
import os, json, boto3
app = Flask(__name__)
# Listen for GET requests to yourdomain.com/account/
@app.route("/account/")
def account():
# Show the account-edit HTML page:
return render_template('account.html')
# Listen for POST requests to yourdomain.com/submit_form/
@app.route("/submit-form/", methods = ["POST"])
def submit_form():
# Collect the data posted from the HTML form in account.html:
username = request.form["username"]
full_name = request.form["full-name"]
avatar_url = request.form["avatar-url"]
# Provide some procedure for storing the new details
update_account(username, full_name, avatar_url)
# Redirect to the user's profile page, if appropriate
return redirect(url_for('profile'))
# Listen for GET requests to yourdomain.com/sign_s3/
#
# Please see https://gist.github.com/RyanBalfanz/f07d827a4818fda0db81 for an example using
# Python 3 for this view.
@app.route('/sign-s3/')
def sign_s3():
# Load necessary information into the application
S3_BUCKET = os.environ.get('S3_BUCKET')
# Load required data from the request
file_name = request.args.get('file-name')
file_type = request.args.get('file-type')
# Initialise the S3 client
s3 = boto3.client('s3')
# Generate and return the presigned URL
presigned_post = s3.generate_presigned_post(
Bucket = S3_BUCKET,
Key = file_name,
Fields = {"acl": "public-read", "Content-Type": file_type},
Conditions = [
{"acl": "public-read"},
{"Content-Type": file_type}
],
ExpiresIn = 3600
)
# Return the data to the client
return json.dumps({
'data': presigned_post,
'url': 'https://%s.s3.amazonaws.com/%s' % (S3_BUCKET, file_name)
})
# Main code
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port = port)