8000 Initial commit for storage sample · aldric/python-docs-samples@cd7aa91 · GitHub
[go: up one dir, main page]

Skip to content

Commit cd7aa91

Browse files
committed
Initial commit for storage sample
1 parent d26b380 commit cd7aa91

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: yes
4+
5+
env_variables:
6+
BUCKET_NAME: "REPLACE THIS WITH YOUR EXISTING BUCKET NAME"
7+
BLOB_NAME: "my-demo-blob"
8+
9+
libraries:
10+
- name: ssl
11+
version: latest
12+
13+
handlers:
14+
- url: /.*
15+
script: main.app
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from google.appengine.ext import vendor
2+
3+
# Add any libraries installed in the "lib" folder.
4+
vendor.add('lib')
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-cloud-storage==1.25.0
2+
Flask==1.1.1

0 commit comments

Comments
 (0)
0