OLD | NEW |
(Empty) | |
| 1 # Copyright 2017 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 """Helper functions to upload data to Google Storage. |
| 6 |
| 7 Text data should be streamed to logdog using |logdog_helper| module. |
| 8 Due to logdog not having image or HTML viewer, those instead should be uploaded |
| 9 to Google Storage directly using this module. |
| 10 """ |
| 11 |
| 12 import logging |
| 13 import os |
| 14 import sys |
| 15 import time |
| 16 |
| 17 from pylib.constants import host_paths |
| 18 from pylib.utils import decorators |
| 19 |
| 20 if host_paths.DEVIL_PATH not in sys.path: |
| 21 sys.path.append(host_paths.DEVIL_PATH) |
| 22 from devil.utils import cmd_helper |
| 23 |
| 24 _GSUTIL_PATH = os.path.join( |
| 25 host_paths.DIR_SOURCE_ROOT, 'third_party', 'catapult', |
| 26 'third_party', 'gsutil', 'gsutil.py') |
| 27 _PUBLIC_URL = 'https://storage.googleapis.com/%s/' |
| 28 _AUTHENTICATED_URL = 'https://storage.cloud.google.com/%s/' |
| 29 |
| 30 |
| 31 @decorators.NoRaiseException(default_return_value='') |
| 32 def upload(name, filepath, bucket, content_type=None, authenticated_link=True): |
| 33 """Uploads data to Google Storage. |
| 34 |
| 35 Args: |
| 36 name: Name of the file on Google Storage. |
| 37 filepath: Path to file you want to upload. |
| 38 bucket: Bucket to upload file to. |
| 39 content_type: Content type to upload as. If not specified, Google storage |
| 40 will attempt to infer content type from file extension. |
| 41 authenticated_link: Whether to return a link that requires user to |
| 42 authenticate with a Google account. Setting this to false will return |
| 43 a link that does not require user to be signed into Google account but |
| 44 will only work for completely public storage buckets. |
| 45 Returns: |
| 46 Web link to item uploaded to Google Storage bucket. |
| 47 """ |
| 48 if bucket.startswith('gs://'): |
| 49 bucket = bucket[len('gs://'):] |
| 50 if bucket.endswith('/'): |
| 51 bucket = bucket[:-1] |
| 52 |
| 53 gs_path = 'gs://%s/%s' % (bucket, name) |
| 54 logging.info('Uploading %s to %s', filepath, gs_path) |
| 55 |
| 56 cmd = [_GSUTIL_PATH] |
| 57 if content_type: |
| 58 cmd.extend(['-h', 'Content-Type:%s' % content_type]) |
| 59 cmd.extend(['cp', filepath, gs_path]) |
| 60 |
| 61 cmd_helper.RunCmd(cmd) |
| 62 |
| 63 url_template = _AUTHENTICATED_URL if authenticated_link else _PUBLIC_URL |
| 64 return os.path.join(url_template % bucket, name) |
| 65 |
| 66 |
| 67 def unique_name(basename, suffix='', timestamp=True, device=None): |
| 68 """Helper function for creating a unique name for a file to store in GS. |
| 69 |
| 70 Args: |
| 71 basename: Base of the unique filename. |
| 72 suffix: Suffix of filename. |
| 73 timestamp: Whether or not to add a timestamp to name. |
| 74 device: Device to add device serial of to name. |
| 75 """ |
| 76 return '%s%s%s%s' % ( |
| 77 basename, |
| 78 '_%s' % time.strftime('%Y_%m_%d_T%H_%M_%S-UTC', time.gmtime()) |
| 79 if timestamp else '', |
| 80 '_%s' % device.serial if device else '', |
| 81 suffix) |
OLD | NEW |