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 _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
| |
28 | |
29 | |
30 @decorators.NoRaiseException(default_return_value='') | |
31 def upload(name, filepath, bucket, content_type=None): | |
32 """Uploads data to Google Storage. | |
33 | |
34 Args: | |
35 name: Name of the file on Google Storage. | |
36 filepath: Path to file you want to upload. | |
37 bucket: Bucket to upload file to. | |
38 """ | |
39 gs_path = 'gs://%s/%s' % (bucket, name) | |
40 logging.info('Uploading %s to %s', filepath, gs_path) | |
41 | |
42 cmd = [_GSUTIL_PATH] | |
43 if content_type: | |
44 cmd.extend(['-h', 'Content-Type:%s' % content_type]) | |
45 cmd.extend(['cp', filepath, gs_path]) | |
46 | |
47 cmd_helper.RunCmd(cmd) | |
48 | |
49 return os.path.join(_URL_TEMPLATE % bucket, name) | |
50 | |
51 | |
52 def unique_name(basename, suffix='', timestamp=True, device=None): | |
53 """Helper function for creating a unique name for a file to store in GS. | |
54 | |
55 Args: | |
56 basename: Base of the unique filename. | |
57 suffix: Suffix of filename. | |
58 timestamp: Whether or not to add a timestamp to name. | |
59 device: Device to add device serial of to name. | |
60 """ | |
61 return '%s%s%s%s' % ( | |
62 basename, | |
63 '_%s' % time.strftime('%Y_%m_%d_T%H_%M_%S', time.localtime()) | |
64 if timestamp else '', | |
65 '_%s' % device.serial if device else '', | |
66 suffix) | |
OLD | NEW |