|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an 'AS IS' BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START functions_http_signed_url] |
| 16 | +from datetime import datetime, timedelta |
| 17 | +# [END functions_http_signed_url] |
| 18 | + |
| 19 | +# [START functions_http_xml] |
| 20 | +import json |
| 21 | +# [END functions_http_xml] |
| 22 | + |
| 23 | +# [START functions_http_form_data] |
| 24 | +import os |
| 25 | +import tempfile |
| 26 | +# [END functions_http_form_data] |
| 27 | + |
| 28 | +# [START functions_http_signed_url] |
| 29 | +from flask import abort |
| 30 | +from google.cloud import storage |
| 31 | +# [END functions_http_signed_url] |
| 32 | + |
| 33 | +# [START functions_http_form_data] |
| 34 | +from werkzeug.utils import secure_filename |
| 35 | +# [END functions_http_form_data] |
| 36 | + |
| 37 | +# [START functions_http_xml] |
| 38 | +import xmltodict |
| 39 | +# [END functions_http_xml] |
| 40 | + |
| 41 | + |
| 42 | +# [START functions_http_xml] |
| 43 | + |
| 44 | +def parse_xml(request): |
| 45 | + """ Parses a document of type 'text/xml' |
| 46 | + Args: |
| 47 | + request (flask.Request): The request object. |
| 48 | + Returns: |
| 49 | + The response text, or any set of values that can be turned into a |
| 50 | + Response object using `make_response` |
| 51 | + <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>. |
| 52 | + """ |
| 53 | + data = xmltodict.parse(request.data) |
| 54 | + return json.dumps(data, indent=2) |
| 55 | +# [END functions_http_xml] |
| 56 | + |
| 57 | + |
| 58 | +# [START functions_http_form_data] |
| 59 | + |
| 60 | +# Helper function that computes the filepath to save files to |
| 61 | +def get_file_path(filename): |
| 62 | + # Note: tempfile.gettempdir() points to an in-memory file system |
| 63 | + # on GCF. Thus, any files in it must fit in the instance's memory. |
| 64 | + file_name = secure_filename(filename) |
| 65 | + return os.path.join(tempfile.gettempdir(), file_name) |
| 66 | + |
| 67 | + |
| 68 | +def parse_multipart(request): |
| 69 | + """ Parses a 'multipart/form-data' upload request |
| 70 | + Args: |
| 71 | + request (flask.Request): The request object. |
| 72 | + Returns: |
| 73 | + The response text, or any set of values that can be turned into a |
| 74 | + Response object using `make_response` |
| 75 | + <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>. |
| 76 | + """ |
| 77 | + |
| 78 | + # This code will process each non-file field in the form |
| 79 | + fields = {} |
| 80 | + data = request.form.to_dict() |
| 81 | + for field in data: |
| 82 | + fields[field] = data[field] |
| 83 | + print('Processed field: %s' % field) |
| 84 | + |
| 85 | + # This code will process each file uploaded |
| 86 | + files = request.files.to_dict() |
| 87 | + for file_name, file in files.items(): |
| 88 | + file.save(get_file_path(file_name)) |
| 89 | + print('Processed file: %s' % file_name) |
| 90 | + |
| 91 | + # Clear temporary directory |
| 92 | + for file_name in files: |
| 93 | + file_path = get_file_path(file_name) |
| 94 | + os.remove(file_path) |
| 95 | + |
| 96 | + return "Done!" |
| 97 | +# [END functions_http_form_data] |
| 98 | + |
| 99 | + |
| 100 | +# [START functions_http_signed_url] |
| 101 | +storage_client = storage.Client() |
| 102 | + |
| 103 | + |
| 104 | +def get_signed_url(request): |
| 105 | + if request.method != 'POST': |
| 106 | + return abort(405) |
| 107 | + |
| 108 | + request_json = request.get_json() |
| 109 | + |
| 110 | + # Get a reference to the destination file in GCS |
| 111 | + file_name = request_json['filename'] |
| 112 | + file = storage_client.bucket('my-bucket').blob(file_name) |
| 113 | + |
| 114 | + # Create a temporary upload URL |
| 115 | + expires_at_ms = datetime.now() + timedelta(seconds=30) |
| 116 | + url = file.generate_signed_url(expires_at_ms, |
| 117 | + content_type=request_json['contentType']) |
| 118 | + |
| 119 | + return url |
| 120 | +# [END functions_http_signed_url] |
0 commit comments