-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Blobstore API with Google Cloud Storage samples #709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
555d089
d5f5d3d
995082c
2d1e959
a5a9f27
f57ed6b
89b10b2
b5a1a07
348e1ec
694ddd9
579875f
fb81c9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
runtime: python27 | ||
api_version: 1 | ||
threadsafe: yes | ||
|
||
handlers: | ||
- url: .* | ||
script: main.app | ||
login: required |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2016 Google Inc. | ||
# | ||
# 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. | ||
|
||
from google.appengine.ext import vendor | ||
|
||
# Add any libraries installed in the "lib" folder. | ||
vendor.add('lib') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""A sample app that operates on GCS files with blobstore API.""" | ||
|
||
import cloudstorage | ||
from google.appengine.api import app_identity | ||
from google.appengine.ext import blobstore | ||
from google.appengine.ext.webapp import blobstore_handlers | ||
import webapp2 | ||
|
||
|
||
# This handler creates a file in Cloud Storage using the cloudstorage | ||
# client library and then reads the data back using the Blobstore API. | ||
|
||
class CreateAndReadFileHandler(webapp2.RequestHandler): | ||
def get(self): | ||
# Get the default Cloud Storage Bucket name and create a file name for | ||
# the object in Cloud Storage. | ||
bucket = app_identity.get_default_gcs_bucket_name() | ||
|
||
# Cloud Storage file names are in the format /bucket/object. | ||
filename = '/{}/blobstore_demo'.format(bucket) | ||
|
||
# Create a file in Google Cloud Storage and write something to it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: sentence needs a period. (throughout please) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
with cloudstorage.open(filename, 'w') as filehandle: | ||
filehandle.write('abcde\n') | ||
|
||
# In order to read the contents of the file using the Blobstore API, | ||
# you must create a blob_key from the Cloud Storage file name. | ||
# Blobstore expects the filename to be in the format of | ||
# /gs/bucket/object | ||
blobstore_filename = '/gs{}'.format(filename) | ||
blob_key = blobstore.create_gs_key(blobstore_filename) | ||
|
||
# Read the file's contents using the Blobstore API. | ||
# The last two parameters specify the start and end index of bytes we | ||
# want to read. | ||
data = blobstore.fetch_data(blob_key, 0, 6) | ||
|
||
# Write the contents to the response. | ||
self.response.headers['Content-Type'] = 'text/plain' | ||
self.response.write(data) | ||
|
||
# Delete the file from Google Cloud Storage using the blob_key. | ||
blobstore.delete(blob_key) | ||
|
||
|
||
# This handler creates a file in Cloud Storage using the cloudstorage | ||
# client library and then serves the file back using the Blobstore API. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary blank line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
class CreateAndServeFileHandler(blobstore_handlers.BlobstoreDownloadHandler): | ||
|
||
def get(self): | ||
# Get the default Cloud Storage Bucket name and create a file name for | ||
# the object in Cloud Storage. | ||
bucket = app_identity.get_default_gcs_bucket_name() | ||
|
||
# Cloud Storage file names are in the format /bucket/object. | ||
filename = '/{}/blobstore_serving_demo'.format(bucket) | ||
|
||
# Create a file in Google Cloud Storage and write something to it | ||
with cloudstorage.open(filename, 'w') as filehandle: | ||
filehandle.write('abcde\n') | ||
|
||
# In order to read the contents of the file using the Blobstore API, | ||
# you must create a blob_key from the Cloud Storage file name. | ||
# Blobstore expects the filename to be in the format of | ||
# /gs/bucket/object | ||
blobstore_filename = '/gs{}'.format(filename) | ||
blob_key = blobstore.create_gs_key(blobstore_filename) | ||
|
||
# BlobstoreDownloadHandler serves the file from Google Cloud Storage to | ||
# your computer using blob_key | ||
self.send_blob(blob_key) | ||
|
||
|
||
app = webapp2.WSGIApplication([ | ||
('/', CreateAndReadFileHandler), | ||
('/blobstore/read', CreateAndReadFileHandler), | ||
('/blobstore/serve', CreateAndServeFileHandler)], debug=True) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2016 Google Inc. All rights reserved. | ||
# | ||
# 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. | ||
|
||
import webtest | ||
|
||
import main | ||
|
||
|
||
def test_create_and_read(testbed, login): | ||
app = webtest.TestApp(main.app) | ||
|
||
login() | ||
response = app.get('/blobstore/read') | ||
|
||
assert 'abcde' in response | ||
|
||
|
||
def test_create_and_serve(testbed, login): | ||
app = webtest.TestApp(main.app) | ||
|
||
login() | ||
response = app.get('/blobstore/serve') | ||
served_file_header = response.headers['X-AppEngine-BlobKey'] | ||
|
||
assert 'encoded_gs_file' in served_file_header | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GoogleAppEngineCloudStorageClient==1.9.22.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary blank line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done