Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 from gcloud import storage | |
| 6 from oauth2client.client import GoogleCredentials | |
| 7 | |
| 8 | |
| 9 class GoogleAPIUtil(object): | |
|
blundell
2016/04/15 13:21:49
How about GoogleStorageAccessor?
droger
2016/04/15 14:17:25
I would like to add other wrappers for task queues
| |
| 10 """Utility class providing helpers for Google APIs. | |
| 11 """ | |
| 12 def __init__(self, project_name, bucket_name): | |
| 13 """project_name is the name of the Google Cloud project. | |
| 14 bucket_name is the name of the bucket that is used for Cloud Storage calls. | |
| 15 """ | |
| 16 print 'Initializing credentials' | |
|
blundell
2016/04/15 13:21:49
Let's nix the print statement now that this is a l
| |
| 17 self._credentials = GoogleCredentials.get_application_default() | |
| 18 self._project_name = project_name | |
| 19 self._bucket_name = bucket_name | |
| 20 | |
| 21 def _GetStorageClient(self): | |
| 22 """Returns the storage client associated with the project""" | |
| 23 return storage.Client(project = self._project_name, | |
| 24 credentials = self._credentials) | |
| 25 | |
| 26 def _GetStorageBucket(self, storage_client): | |
| 27 return storage_client.get_bucket(self._bucket_name) | |
| 28 | |
| 29 def UploadFile(self, filename_src, filename_dest): | |
| 30 """Uploads a file to Google Cloud Storage | |
| 31 | |
| 32 Args: | |
| 33 filename_src: name of the local file | |
| 34 filename_dest: name of the file in Google Cloud Storage | |
| 35 | |
| 36 Returns: | |
| 37 The URL of the file in Google Cloud Storage. | |
| 38 """ | |
| 39 client = self._GetStorageClient() | |
| 40 bucket = self._GetStorageBucket(client) | |
| 41 blob = bucket.blob(filename_dest) | |
| 42 with open(filename_src) as file_src: | |
| 43 blob.upload_from_file(file_src) | |
| 44 return blob.public_url | |
| 45 | |
| 46 def UploadString(self, data_string, filename_dest): | |
| 47 """Uploads a string to Google Cloud Storage | |
| 48 | |
| 49 Args: | |
| 50 data_string: the contents of the file to be uploaded | |
| 51 filename_dest: name of the file in Google Cloud Storage | |
| 52 | |
| 53 Returns: | |
| 54 The URL of the file in Google Cloud Storage. | |
| 55 """ | |
| 56 client = self._GetStorageClient() | |
| 57 bucket = self._GetStorageBucket(client) | |
| 58 blob = bucket.blob(filename_dest) | |
| 59 blob.upload_from_string(data_string) | |
| 60 return blob.public_url | |
| 61 | |
| OLD | NEW |