Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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 9 commits into from
Feb 13, 2017

Conversation

ryanmats
Copy link
Contributor

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.

@ryanmats ryanmats requested a review from theacodes February 10, 2017 01:15
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Feb 10, 2017
import logging
import os

import cloudstorage as gcs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't alias imports.

Copy link
Contributor Author

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(
Copy link
Contributor

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.

Copy link
Contributor Author

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(
Copy link
Contributor

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.

Copy link
Contributor Author

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:
Copy link
Contributor

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.

Copy link
Contributor Author

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:
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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(
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, done

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.
Copy link
Contributor

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.

Copy link
Contributor Author

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):
Copy link
Contributor

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

Copy link
Contributor Author

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!')
Copy link
Contributor

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.

@theacodes
Copy link
Contributor

@ryanmats let me know if you have these passing locally.

@ryanmats
Copy link
Contributor Author

@jonparrott yep, they are passing locally

@theacodes theacodes merged commit d5d402d into master Feb 13, 2017
@theacodes theacodes deleted the appengine-cloud-storage-client branch February 13, 2017 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.