Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Unified Diff: tools/android/loading/gce/google_storage_accessor.py

Issue 1882793006: tools/android/loading Split GoogleAPI wrappers to a separate library (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/android/loading/gce/main.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/android/loading/gce/google_storage_accessor.py
diff --git a/tools/android/loading/gce/google_storage_accessor.py b/tools/android/loading/gce/google_storage_accessor.py
new file mode 100644
index 0000000000000000000000000000000000000000..59c47da3ec04976fa2d1ee6dc428ce11727144df
--- /dev/null
+++ b/tools/android/loading/gce/google_storage_accessor.py
@@ -0,0 +1,60 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+from gcloud import storage
+from oauth2client.client import GoogleCredentials
+
+
+class GoogleStorageAccessor(object):
+ """Utility class providing helpers for Google Cloud Storage.
+ """
+ def __init__(self, project_name, bucket_name):
+ """project_name is the name of the Google Cloud project.
+ bucket_name is the name of the bucket that is used for Cloud Storage calls.
+ """
+ self._credentials = GoogleCredentials.get_application_default()
+ self._project_name = project_name
+ self._bucket_name = bucket_name
+
+ def _GetStorageClient(self):
+ """Returns the storage client associated with the project"""
+ return storage.Client(project = self._project_name,
+ credentials = self._credentials)
+
+ def _GetStorageBucket(self, storage_client):
+ return storage_client.get_bucket(self._bucket_name)
+
+ def UploadFile(self, filename_src, filename_dest):
+ """Uploads a file to Google Cloud Storage
+
+ Args:
+ filename_src: name of the local file
+ filename_dest: name of the file in Google Cloud Storage
+
+ Returns:
+ The URL of the file in Google Cloud Storage.
+ """
+ client = self._GetStorageClient()
+ bucket = self._GetStorageBucket(client)
+ blob = bucket.blob(filename_dest)
+ with open(filename_src) as file_src:
+ blob.upload_from_file(file_src)
+ return blob.public_url
+
+ def UploadString(self, data_string, filename_dest):
+ """Uploads a string to Google Cloud Storage
+
+ Args:
+ data_string: the contents of the file to be uploaded
+ filename_dest: name of the file in Google Cloud Storage
+
+ Returns:
+ The URL of the file in Google Cloud Storage.
+ """
+ client = self._GetStorageClient()
+ bucket = self._GetStorageBucket(client)
+ blob = bucket.blob(filename_dest)
+ blob.upload_from_string(data_string)
+ return blob.public_url
+
« no previous file with comments | « no previous file | tools/android/loading/gce/main.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698