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 from gcloud import storage |
6 from oauth2client.client import GoogleCredentials | |
7 | 6 |
8 | 7 |
9 class GoogleStorageAccessor(object): | 8 class GoogleStorageAccessor(object): |
10 """Utility class providing helpers for Google Cloud Storage. | 9 """Utility class providing helpers for Google Cloud Storage. |
11 """ | 10 """ |
12 def __init__(self, project_name, bucket_name): | 11 def __init__(self, credentials, project_name, bucket_name): |
13 """project_name is the name of the Google Cloud project. | 12 """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. | 13 bucket_name is the name of the bucket that is used for Cloud Storage calls. |
15 """ | 14 """ |
16 self._credentials = GoogleCredentials.get_application_default() | 15 self._credentials = credentials |
17 self._project_name = project_name | 16 self._project_name = project_name |
18 self._bucket_name = bucket_name | 17 self._bucket_name = bucket_name |
19 | 18 |
20 def _GetStorageClient(self): | 19 def _GetStorageClient(self): |
21 """Returns the storage client associated with the project""" | 20 """Returns the storage client associated with the project""" |
22 return storage.Client(project = self._project_name, | 21 return storage.Client(project = self._project_name, |
23 credentials = self._credentials) | 22 credentials = self._credentials) |
24 | 23 |
25 def _GetStorageBucket(self, storage_client): | 24 def _GetStorageBucket(self, storage_client): |
26 return storage_client.get_bucket(self._bucket_name) | 25 return storage_client.get_bucket(self._bucket_name) |
(...skipping 24 matching lines...) Expand all Loading... |
51 | 50 |
52 Returns: | 51 Returns: |
53 The URL of the file in Google Cloud Storage. | 52 The URL of the file in Google Cloud Storage. |
54 """ | 53 """ |
55 client = self._GetStorageClient() | 54 client = self._GetStorageClient() |
56 bucket = self._GetStorageBucket(client) | 55 bucket = self._GetStorageBucket(client) |
57 blob = bucket.blob(filename_dest) | 56 blob = bucket.blob(filename_dest) |
58 blob.upload_from_string(data_string) | 57 blob.upload_from_string(data_string) |
59 return blob.public_url | 58 return blob.public_url |
60 | 59 |
OLD | NEW |