-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (43 loc) · 1.7 KB
/
main.py
File metadata and controls
56 lines (43 loc) · 1.7 KB
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
import datetime
from flask import Flask, request, render_template, url_for, redirect
from google.oauth2 import service_account
import os
import requests
from google.cloud import storage
from werkzeug.utils import secure_filename
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/key.json'
EXPIRATION = datetime.timedelta(hours=60)
FILE_TYPE = 'application/pdf'
BUCKET = 'BUCKET_NAME'
def upload_via_signed(bucket_name, blob_name, filename, expiration, file_type):
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)
signed_url = blob.generate_signed_url(version="v4", method='PUT', expiration=expiration, content_type=file_type)
print(signed_url)
"""
print(
"curl -X PUT -H 'Content-Type: application/octet-stream' "
"--upload-file my-file '{}'".format(signed_url)
)
"""
requests.put(signed_url, open(filename.filename, 'rb'), headers={'Content-Type': file_type})
app = Flask(__name__)
@app.route('/')
def homepage():
return render_template('home.html')
@app.route('/', methods = ['POST'])
def upload_file():
if request.method == 'POST':
diag = request.files['file']
filename_1 = secure_filename(diag.filename)
diag.save(filename_1)
print(diag)
upload_via_signed(BUCKET, 'diag', diag, EXPIRATION, FILE_TYPE)
os.remove(filename_1)
return "done"
if __name__ == "__main__":
# Used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
app.run(host="0.0.0.0", port=8080, debug=True)