-
Notifications
You must be signed in to change notification settings - Fork 6.6k
App Engine Cloud Storage Client Sample #793
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fe89e2f
Added AppEngine Storage Client sample, renamed subfolders
ryanmats e244bb1
Merge branch 'master' of https://github.com/GoogleCloudPlatform/pytho…
ryanmats 3ba4d51
Merge branch 'master' of https://github.com/GoogleCloudPlatform/pytho…
ryanmats 603df4b
Fixed code review issues
ryanmats 6e8807a
Updated storage api-client code to upload/delete objects - now testin…
ryanmats b06fe1b
Merge branch 'master' of https://github.com/GoogleCloudPlatform/pytho…
ryanmats 4e40943
Changed BUCKET_NAME back to <your-bucket-name>
ryanmats 897dd07
Fixed code review issues
ryanmats 15fd8f3
Merge branch 'master' of https://github.com/GoogleCloudPlatform/pytho…
ryanmats File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
runtime: python27 | ||
api_version: 1 | ||
threadsafe: yes | ||
|
||
env_variables: | ||
|
||
handlers: | ||
- url: /blobstore.* | ||
script: blobstore.app | ||
|
||
- url: /.* | ||
script: main.app |
4 changes: 4 additions & 0 deletions
4
appengine/standard/storage/appengine-client/appengine_config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from google.appengine.ext import vendor | ||
|
||
# Add any libraries installed in the "lib" folder. | ||
vendor.add('lib') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2017 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. | ||
|
||
# [START sample] | ||
"""A sample app that uses GCS client to operate on bucket and file.""" | ||
|
||
# [START imports] | ||
import os | ||
|
||
import cloudstorage | ||
from google.appengine.api import app_identity | ||
|
||
import webapp2 | ||
|
||
# [END imports] | ||
|
||
# [START retries] | ||
cloudstorage.set_default_retry_params( | ||
cloudstorage.RetryParams( | ||
initial_delay=0.2, max_delay=5.0, backoff_factor=2, max_retry_period=15 | ||
)) | ||
# [END retries] | ||
|
||
|
||
class MainPage(webapp2.RequestHandler): | ||
"""Main page for GCS demo application.""" | ||
|
||
# [START get_default_bucket] | ||
def get(self): | ||
bucket_name = os.environ.get( | ||
'BUCKET_NAME', app_identity.get_default_gcs_bucket_name()) | ||
|
||
self.response.headers['Content-Type'] = 'text/plain' | ||
self.response.write( | ||
'Demo GCS Application running from Version: {}\n'.format( | ||
os.environ['CURRENT_VERSION_ID'])) | ||
self.response.write('Using bucket name: \n\n'.format(bucket_name)) | ||
# [END get_default_bucket] | ||
|
||
bucket = '/' + bucket_name | ||
filename = bucket + '/demo-testfile' | ||
self.tmp_filenames_to_clean_up = [] | ||
|
||
self.create_file(filename) | ||
self.response.write('\n\n') | ||
|
||
self.read_file(filename) | ||
self.response.write('\n\n') | ||
|
||
self.stat_file(filename) | ||
self.response.write('\n\n') | ||
|
||
self.create_files_for_list_bucket(bucket) | ||
self.response.write('\n\n') | ||
|
||
self.list_bucket(bucket) | ||
self.response.write('\n\n') | ||
|
||
self.list_bucket_directory_mode(bucket) | ||
self.response.write('\n\n') | ||
|
||
self.delete_files() | ||
self.response.write('\n\nThe demo ran successfully!\n') | ||
|
||
# [START write] | ||
def create_file(self, filename): | ||
"""Create a file.""" | ||
|
||
self.response.write('Creating file {}\n'.format(filename)) | ||
|
||
# The retry_params specified in the open call will override the default | ||
# retry params for this particular file handle. | ||
write_retry_params = cloudstorage.RetryParams(backoff_factor=1.1) | ||
with cloudstorage.open( | ||
filename, 'w', content_type='text/plain', options={ | ||
'x-goog-meta-foo': 'foo', 'x-goog-meta-bar': 'bar'}, | ||
retry_params=write_retry_params) as cloudstorage_file: | ||
cloudstorage_file.write('abcde\n') | ||
cloudstorage_file.write('f'*1024*4 + '\n') | ||
self.tmp_filenames_to_clean_up.append(filename) | ||
# [END write] | ||
|
||
# [START read] | ||
def read_file(self, filename): | ||
self.response.write( | ||
'Abbreviated file content (first line and last 1K):\n') | ||
|
||
with cloudstorage.open(filename) as cloudstorage_file: | ||
self.response.write(cloudstorage_file.readline()) | ||
cloudstorage_file.seek(-1024, os.SEEK_END) | ||
self.response.write(cloudstorage_file.read()) | ||
# [END read] | ||
|
||
def stat_file(self, filename): | ||
self.response.write('File stat:\n') | ||
|
||
stat = cloudstorage.stat(filename) | ||
self.response.write(repr(stat)) | ||
|
||
def create_files_for_list_bucket(self, bucket): | ||
self.response.write('Creating more files for listbucket...\n') | ||
filenames = [bucket + n for n in [ | ||
'/foo1', '/foo2', '/bar', '/bar/1', '/bar/2', '/boo/']] | ||
for f in filenames: | ||
self.create_file(f) | ||
|
||
# [START list_bucket] | ||
def list_bucket(self, bucket): | ||
"""Create several files and paginate through them.""" | ||
|
||
self.response.write('Listbucket result:\n') | ||
|
||
# Production apps should set page_size to a practical value. | ||
page_size = 1 | ||
stats = cloudstorage.listbucket(bucket + '/foo', max_keys=page_size) | ||
while True: | ||
count = 0 | ||
for stat in stats: | ||
count += 1 | ||
self.response.write(repr(stat)) | ||
self.response.write('\n') | ||
|
||
if count != page_size or count == 0: | ||
break | ||
stats = cloudstorage.listbucket( | ||
bucket + '/foo', max_keys=page_size, marker=stat.filename) | ||
# [END list_bucket] | ||
|
||
def list_bucket_directory_mode(self, bucket): | ||
self.response.write('Listbucket directory mode result:\n') | ||
for stat in cloudstorage.listbucket(bucket + '/b', delimiter='/'): | ||
self.response.write(stat) | ||
self.response.write('\n') | ||
if stat.is_dir: | ||
for subdir_file in cloudstorage.listbucket( | ||
stat.filename, delimiter='/'): | ||
self.response.write(' {}'.format(subdir_file)) | ||
self.response.write('\n') | ||
|
||
# [START delete_files] | ||
def delete_files(self): | ||
self.response.write('Deleting files...\n') | ||
for filename in self.tmp_filenames_to_clean_up: | ||
self.response.write('Deleting file {}\n'.format(filename)) | ||
try: | ||
cloudstorage.delete(filename) | ||
except cloudstorage.NotFoundError: | ||
pass | ||
# [END delete_files] | ||
|
||
|
||
app = webapp2.WSGIApplication( | ||
[('/', MainPage)], debug=True) | ||
# [END sample] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright 2017 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_get(testbed, cloud_config): | ||
main.BUCKET_NAME = cloud_config.project | ||
app = webtest.TestApp(main.app) | ||
|
||
response = app.get('/') | ||
|
||
assert response.status_int == 200 | ||
assert 'The demo ran successfully!' in response.body |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GoogleAppEngineCloudStorageClient==1.9.22.1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
use
.format
instead of string concatenation.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