-
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
Conversation
…n-docs-samples into appengine-cloud-storage-client
import logging | ||
import os | ||
|
||
import cloudstorage as gcs |
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.
Don't alias imports.
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
# [END imports] | ||
|
||
# [START retries] | ||
my_default_retry_params = gcs.RetryParams( |
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.
instead of having a temporary variable, just pass this right into set_default_retry_params
.
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
'BUCKET_NAME', app_identity.get_default_gcs_bucket_name()) | ||
|
||
self.response.headers['Content-Type'] = 'text/plain' | ||
self.response.write( |
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
self.list_bucket_directory_mode(bucket) | ||
self.response.write('\n\n') | ||
|
||
except Exception, e: |
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.
Don't catch broad exceptions. In this case, it's better to just not handle errors and let them bubble up to the user.
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
The retry_params specified in the open call will override the default | ||
retry params for this particular file handle. | ||
|
||
Args: |
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.
Just remove this args section.
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
def create_file(self, filename): | ||
"""Create a file. | ||
|
||
The retry_params specified in the open call will override the default |
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.
Move this to a comment above write_retry_commands
.
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
self.response.write('Creating file %s\n' % filename) | ||
|
||
write_retry_params = gcs.RetryParams(backoff_factor=1.1) | ||
gcs_file = gcs.open( |
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.
Does this support with
? If so, switch to that.
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.
yep, done
…n-docs-samples into appengine-cloud-storage-client
from oauth2client.client import GoogleCredentials | ||
import webapp2 | ||
|
||
|
||
# The bucket that will be used to list objects. | ||
BUCKET_NAME = '<your-bucket-name>' | ||
|
||
# The filename for an object to be uploaded. |
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.
I'd actually prefer this to just be in line in the code below.
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
credentials = GoogleCredentials.get_application_default() | ||
storage = discovery.build('storage', 'v1', credentials=credentials) | ||
|
||
|
||
class MainPage(webapp2.RequestHandler): | ||
def upload_object(self, bucket, filename): |
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 file_obj
instead of filename
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
def get(self): | ||
response = storage.objects().list(bucket=BUCKET_NAME).execute() | ||
string_io_file = StringIO.StringIO() | ||
string_io_file.write('Hello World!') |
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.
You can just pass this string into the StringIO
constructor.
…n-docs-samples into appengine-cloud-storage-client
@ryanmats let me know if you have these passing locally. |
@jonparrott yep, they are passing locally |
Moved over code from https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/app-engine-cloud-storage-sample to python-docs-samples , added a test, and fixed style issues.