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 _AUTHENICATED_URL = 'https://storage.cloud.google.com/%s/' | |
the real yoland
2017/05/03 22:23:38
super nit: similar as below
| |
29 | |
30 | |
31 @decorators.NoRaiseException(default_return_value='') | |
32 def upload(name, filepath, bucket, content_type=None, authenticated_link=True): | |
the real yoland
2017/05/03 22:23:39
super nit: maybe link_to_authenticate? authenticat
mikecase (-- gone --)
2017/05/03 22:45:18
Ack. I think authenticated_link is still less awkw
| |
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 only | |
43 work when uploading to public storage buckets. | |
the real yoland
2017/05/03 22:23:38
nit: as you mentioned, we might want to always use
mikecase (-- gone --)
2017/05/03 22:45:18
Improved comment. And have default for authenticat
| |
44 Returns: | |
45 Web link to item uploaded to Google Storage bucket. | |
46 """ | |
47 if bucket.startswith('gs://'): | |
48 bucket = bucket[len('gs://'):] | |
49 if bucket.endswith('/'): | |
50 bucket = bucket[:-1] | |
51 | |
52 gs_path = 'gs://%s/%s' % (bucket, name) | |
53 logging.info('Uploading %s to %s', filepath, gs_path) | |
54 | |
55 cmd = [_GSUTIL_PATH] | |
56 if content_type: | |
57 cmd.extend(['-h', 'Content-Type:%s' % content_type]) | |
58 cmd.extend(['cp', filepath, gs_path]) | |
59 | |
60 cmd_helper.RunCmd(cmd) | |
61 | |
62 url_template = _AUTHENICATED_URL if authenticated_link else _PUBLIC_URL | |
63 return os.path.join(url_template % bucket, name) | |
64 | |
65 | |
66 def unique_name(basename, suffix='', timestamp=True, device=None): | |
67 """Helper function for creating a unique name for a file to store in GS. | |
68 | |
69 Args: | |
70 basename: Base of the unique filename. | |
71 suffix: Suffix of filename. | |
72 timestamp: Whether or not to add a timestamp to name. | |
73 device: Device to add device serial of to name. | |
74 """ | |
75 return '%s%s%s%s' % ( | |
76 basename, | |
77 '_%s' % time.strftime('%Y_%m_%d_T%H_%M_%S', time.localtime()) | |
the real yoland
2017/05/03 22:23:38
hmm, should this use UTC time?
mikecase (-- gone --)
2017/05/03 22:45:18
probably! Done
| |
78 if timestamp else '', | |
79 '_%s' % device.serial if device else '', | |
80 suffix) | |
OLD | NEW |