1
+ # Copyright 2020 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
+ from flask import Flask , make_response
16
+ import os
17
+
18
+ from google .cloud import storage
19
+
20
+
21
+ app = Flask (__name__ )
22
+
23
+
24
+ @app .route ('/' , methods = ['GET' ])
25
+ def get ():
26
+ bucket_name = os .environ ['BUCKET_NAME' ]
27
+ blob_name = os .environ ['BLOB_NAME' ]
28
+
29
+ client = storage .Client ()
30
+ bucket = client .bucket (bucket_name )
31
+ blob = bucket .blob (blob_name )
32
+
33
+ response_text = ''
34
+
35
+ text_to_store = b'abcde\n ' + b'f' * 1024 * 4 + b'\n '
36
+ blob .upload_from_string (text_to_store )
37
+ response_text += 'Stored text in a blob.\n \n '
38
+
39
+ stored_contents = blob .download_as_string ()
40
+ if stored_contents == text_to_store :
41
+ response_text += 'Downloaded text matches uploaded text.\n \n '
42
+ else :
43
+ response_text += 'Downloaded text DOES NOT MATCH uploaded text!\n \n '
44
+
45
+ response_text += 'Blobs in the bucket:\n '
46
+ for blob in client .list_blobs (bucket_name ):
47
+ response_text += ' ' + blob .id + '\n '
48
+ response_text += '\n '
49
+
50
+ bucket .delete_blob (blob_name )
51
+ response_text += 'Blob ' + blob_name + ' deleted.\n '
52
+
53
+ response = make_response (response_text , 200 )
54
+ response .mimetype = 'text/plain'
55
+ return response
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+ return 'Hello there' , 200
64
+
65
+
66
+ if __name__ == '__main__' :
67
+ # This is used when running locally.
68
+ app .run (host = '127.0.0.1' , port = 8080 , debug = True )
0 commit comments