forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
120 lines (94 loc) · 3.55 KB
/
Copy pathmain.py
File metadata and controls
120 lines (94 loc) · 3.55 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
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
# Copyright 2018 Google LLC
#
# 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.
# [START functions_http_signed_url]
from datetime import datetime, timedelta
# [END functions_http_signed_url]
# [START functions_http_xml]
import json
# [END functions_http_xml]
# [START functions_http_form_data]
import os
import tempfile
# [END functions_http_form_data]
# [START functions_http_signed_url]
from flask import abort
from google.cloud import storage
# [END functions_http_signed_url]
# [START functions_http_form_data]
from werkzeug.utils import secure_filename
# [END functions_http_form_data]
# [START functions_http_xml]
import xmltodict
# [END functions_http_xml]
# [START functions_http_xml]
def parse_xml(request):
""" Parses a document of type 'text/xml'
Args:
request (flask.Request): The request object.
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>.
"""
data = xmltodict.parse(request.data)
return json.dumps(data, indent=2)
# [END functions_http_xml]
# [START functions_http_form_data]
# Helper function that computes the filepath to save files to
def get_file_path(filename):
# Note: tempfile.gettempdir() points to an in-memory file system
# on GCF. Thus, any files in it must fit in the instance's memory.
file_name = secure_filename(filename)
return os.path.join(tempfile.gettempdir(), file_name)
def parse_multipart(request):
""" Parses a 'multipart/form-data' upload request
Args:
request (flask.Request): The request object.
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>.
"""
# This code will process each non-file field in the form
fields = {}
data = request.form.to_dict()
for field in data:
fields[field] = data[field]
print('Processed field: %s' % field)
# This code will process each file uploaded
files = request.files.to_dict()
for file_name, file in files.items():
file.save(get_file_path(file_name))
print('Processed file: %s' % file_name)
# Clear temporary directory
for file_name in files:
file_path = get_file_path(file_name)
os.remove(file_path)
return "Done!"
# [END functions_http_form_data]
# [START functions_http_signed_url]
storage_client = storage.Client()
def get_signed_url(request):
if request.method != 'POST':
return abort(405)
request_json = request.get_json()
# Get a reference to the destination file in GCS
file_name = request_json['filename']
file = storage_client.bucket('my-bucket').blob(file_name)
# Create a temporary upload URL
expires_at_ms = datetime.now() + timedelta(seconds=30)
url = file.generate_signed_url(expires_at_ms,
content_type=request_json['contentType'])
return url
# [END functions_http_signed_url]