Index: build/android/pylib/utils/google_storage_helper.py |
diff --git a/build/android/pylib/utils/google_storage_helper.py b/build/android/pylib/utils/google_storage_helper.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ce5f4e47c41f37b771702542b8dfeef0d85608c5 |
--- /dev/null |
+++ b/build/android/pylib/utils/google_storage_helper.py |
@@ -0,0 +1,66 @@ |
+# Copyright 2017 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. |
+ |
+"""Helper functions to upload data to Google Storage. |
+ |
+Text data should be streamed to logdog using |logdog_helper| module. |
+Due to logdog not having image or HTML viewer, those instead should be uploaded |
+to Google Storage directly using this module. |
+""" |
+ |
+import logging |
+import os |
+import sys |
+import time |
+ |
+from pylib.constants import host_paths |
+from pylib.utils import decorators |
+ |
+if host_paths.DEVIL_PATH not in sys.path: |
+ sys.path.append(host_paths.DEVIL_PATH) |
+from devil.utils import cmd_helper |
+ |
+_GSUTIL_PATH = os.path.join( |
+ host_paths.DIR_SOURCE_ROOT, 'third_party', 'catapult', |
+ 'third_party', 'gsutil', 'gsutil.py') |
+_URL_TEMPLATE = 'https://storage.googleapis.com/%s/' |
jbudorick
2017/04/11 00:36:44
Zhiling, which one of these URLs were we having is
BigBossZhiling
2017/04/11 16:21:33
storage.googleapis.com is the one that cannot have
mikecase (-- gone --)
2017/04/26 18:01:52
Ack. From...
https://cloud.google.com/storage/doc
|
+ |
+ |
+@decorators.NoRaiseException(default_return_value='') |
+def upload(name, filepath, bucket, content_type=None): |
+ """Uploads data to Google Storage. |
+ |
+ Args: |
+ name: Name of the file on Google Storage. |
+ filepath: Path to file you want to upload. |
+ bucket: Bucket to upload file to. |
+ """ |
+ gs_path = 'gs://%s/%s' % (bucket, name) |
+ logging.info('Uploading %s to %s', filepath, gs_path) |
+ |
+ cmd = [_GSUTIL_PATH] |
+ if content_type: |
+ cmd.extend(['-h', 'Content-Type:%s' % content_type]) |
+ cmd.extend(['cp', filepath, gs_path]) |
+ |
+ cmd_helper.RunCmd(cmd) |
+ |
+ return os.path.join(_URL_TEMPLATE % bucket, name) |
+ |
+ |
+def unique_name(basename, suffix='', timestamp=True, device=None): |
+ """Helper function for creating a unique name for a file to store in GS. |
+ |
+ Args: |
+ basename: Base of the unique filename. |
+ suffix: Suffix of filename. |
+ timestamp: Whether or not to add a timestamp to name. |
+ device: Device to add device serial of to name. |
+ """ |
+ return '%s%s%s%s' % ( |
+ basename, |
+ '_%s' % time.strftime('%Y_%m_%d_T%H_%M_%S', time.localtime()) |
+ if timestamp else '', |
+ '_%s' % device.serial if device else '', |
+ suffix) |