| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from gcloud import storage | 5 import gcloud.exceptions |
| 6 import gcloud.storage |
| 6 | 7 |
| 7 | 8 |
| 8 class GoogleStorageAccessor(object): | 9 class GoogleStorageAccessor(object): |
| 9 """Utility class providing helpers for Google Cloud Storage. | 10 """Utility class providing helpers for Google Cloud Storage. |
| 10 """ | 11 """ |
| 11 def __init__(self, credentials, project_name, bucket_name): | 12 def __init__(self, credentials, project_name, bucket_name): |
| 12 """project_name is the name of the Google Cloud project. | 13 """project_name is the name of the Google Cloud project. |
| 13 bucket_name is the name of the bucket that is used for Cloud Storage calls. | 14 bucket_name is the name of the bucket that is used for Cloud Storage calls. |
| 14 """ | 15 """ |
| 15 self._credentials = credentials | 16 self._credentials = credentials |
| 16 self._project_name = project_name | 17 self._project_name = project_name |
| 17 self._bucket_name = bucket_name | 18 self._bucket_name = bucket_name |
| 18 | 19 |
| 19 def _GetStorageClient(self): | 20 def _GetStorageClient(self): |
| 20 """Returns the storage client associated with the project""" | 21 """Returns the storage client associated with the project""" |
| 21 return storage.Client(project = self._project_name, | 22 return gcloud.storage.Client(project = self._project_name, |
| 22 credentials = self._credentials) | 23 credentials = self._credentials) |
| 23 | 24 |
| 24 def _GetStorageBucket(self, storage_client): | 25 def _GetStorageBucket(self, storage_client): |
| 25 return storage_client.get_bucket(self._bucket_name) | 26 return storage_client.get_bucket(self._bucket_name) |
| 26 | 27 |
| 28 def DownloadAsString(self, remote_filename): |
| 29 """Returns the content of a remote file as a string, or None if the file |
| 30 does not exist. |
| 31 """ |
| 32 client = self._GetStorageClient() |
| 33 bucket = self._GetStorageBucket(client) |
| 34 blob = bucket.get_blob(remote_filename) |
| 35 if not blob: |
| 36 return None |
| 37 try: |
| 38 return blob.download_as_string() |
| 39 except gcloud.exceptions.NotFound: |
| 40 return None |
| 41 |
| 27 def UploadFile(self, filename_src, filename_dest): | 42 def UploadFile(self, filename_src, filename_dest): |
| 28 """Uploads a file to Google Cloud Storage | 43 """Uploads a file to Google Cloud Storage |
| 29 | 44 |
| 30 Args: | 45 Args: |
| 31 filename_src: name of the local file | 46 filename_src: name of the local file |
| 32 filename_dest: name of the file in Google Cloud Storage | 47 filename_dest: name of the file in Google Cloud Storage |
| 33 | 48 |
| 34 Returns: | 49 Returns: |
| 35 The URL of the file in Google Cloud Storage. | 50 The URL of the file in Google Cloud Storage. |
| 36 """ | 51 """ |
| (...skipping 13 matching lines...) Expand all Loading... |
| 50 | 65 |
| 51 Returns: | 66 Returns: |
| 52 The URL of the file in Google Cloud Storage. | 67 The URL of the file in Google Cloud Storage. |
| 53 """ | 68 """ |
| 54 client = self._GetStorageClient() | 69 client = self._GetStorageClient() |
| 55 bucket = self._GetStorageBucket(client) | 70 bucket = self._GetStorageBucket(client) |
| 56 blob = bucket.blob(filename_dest) | 71 blob = bucket.blob(filename_dest) |
| 57 blob.upload_from_string(data_string) | 72 blob.upload_from_string(data_string) |
| 58 return blob.public_url | 73 return blob.public_url |
| 59 | 74 |
| OLD | NEW |