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/' | |
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: | |
jbudorick
2017/04/29 01:12:26
Add a returns section.
mikecase (-- gone --)
2017/05/03 21:25:14
Done
| |
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. | |
44 """ | |
45 if bucket.startswith('gs://'): | |
46 bucket = bucket[len('gs://'):] | |
47 if bucket.endswith('/'): | |
48 bucket = bucket[:-1] | |
49 | |
50 gs_path = 'gs://%s/%s' % (bucket, name) | |
51 logging.info('Uploading %s to %s', filepath, gs_path) | |
52 | |
53 cmd = [_GSUTIL_PATH] | |
54 if content_type: | |
55 cmd.extend(['-h', 'Content-Type:%s' % content_type]) | |
56 cmd.extend(['cp', filepath, gs_path]) | |
57 | |
58 cmd_helper.RunCmd(cmd) | |
59 | |
60 url_template = _AUTHENICATED_URL if authenticated_link else _PUBLIC_URL | |
61 return os.path.join(url_template % bucket, name) | |
62 | |
63 | |
64 def unique_name(basename, suffix='', timestamp=True, device=None): | |
65 """Helper function for creating a unique name for a file to store in GS. | |
66 | |
67 Args: | |
68 basename: Base of the unique filename. | |
69 suffix: Suffix of filename. | |
70 timestamp: Whether or not to add a timestamp to name. | |
71 device: Device to add device serial of to name. | |
72 """ | |
73 return '%s%s%s%s' % ( | |
74 basename, | |
75 '_%s' % time.strftime('%Y_%m_%d_T%H_%M_%S', time.localtime()) | |
jbudorick
2017/04/29 01:12:26
either pull this out of line or wrap it in parens.
mikecase (-- gone --)
2017/05/03 21:25:14
Done
| |
76 if timestamp else '', | |
77 '_%s' % device.serial if device else '', | |
78 suffix) | |
OLD | NEW |