OLD | NEW |
1 # Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Helper functions to upload data to Google Storage. | 5 """Helper functions to upload data to Google Storage. |
6 | 6 |
7 Text data should be streamed to logdog using |logdog_helper| module. | 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 | 8 Due to logdog not having image or HTML viewer, those instead should be uploaded |
9 to Google Storage directly using this module. | 9 to Google Storage directly using this module. |
10 """ | 10 """ |
11 | 11 |
| 12 import hashlib |
12 import logging | 13 import logging |
13 import os | 14 import os |
14 import sys | 15 import sys |
15 import time | 16 import time |
16 | 17 |
17 from pylib.constants import host_paths | 18 from pylib.constants import host_paths |
18 from pylib.utils import decorators | 19 from pylib.utils import decorators |
19 | 20 |
20 if host_paths.DEVIL_PATH not in sys.path: | 21 if host_paths.DEVIL_PATH not in sys.path: |
21 sys.path.append(host_paths.DEVIL_PATH) | 22 sys.path.append(host_paths.DEVIL_PATH) |
(...skipping 16 matching lines...) Expand all Loading... |
38 bucket: Bucket to upload file to. | 39 bucket: Bucket to upload file to. |
39 content_type: Content type to upload as. If not specified, Google storage | 40 content_type: Content type to upload as. If not specified, Google storage |
40 will attempt to infer content type from file extension. | 41 will attempt to infer content type from file extension. |
41 authenticated_link: Whether to return a link that requires user to | 42 authenticated_link: Whether to return a link that requires user to |
42 authenticate with a Google account. Setting this to false will return | 43 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 a link that does not require user to be signed into Google account but |
44 will only work for completely public storage buckets. | 45 will only work for completely public storage buckets. |
45 Returns: | 46 Returns: |
46 Web link to item uploaded to Google Storage bucket. | 47 Web link to item uploaded to Google Storage bucket. |
47 """ | 48 """ |
48 if bucket.startswith('gs://'): | 49 bucket = _format_bucket_name(bucket) |
49 bucket = bucket[len('gs://'):] | |
50 if bucket.endswith('/'): | |
51 bucket = bucket[:-1] | |
52 | 50 |
53 gs_path = 'gs://%s/%s' % (bucket, name) | 51 gs_path = 'gs://%s/%s' % (bucket, name) |
54 logging.info('Uploading %s to %s', filepath, gs_path) | 52 logging.info('Uploading %s to %s', filepath, gs_path) |
55 | 53 |
56 cmd = [_GSUTIL_PATH, '-q'] | 54 cmd = [_GSUTIL_PATH, '-q'] |
57 if content_type: | 55 if content_type: |
58 cmd.extend(['-h', 'Content-Type:%s' % content_type]) | 56 cmd.extend(['-h', 'Content-Type:%s' % content_type]) |
59 cmd.extend(['cp', filepath, gs_path]) | 57 cmd.extend(['cp', filepath, gs_path]) |
60 | 58 |
61 cmd_helper.RunCmd(cmd) | 59 cmd_helper.RunCmd(cmd) |
62 | 60 |
63 url_template = _AUTHENTICATED_URL if authenticated_link else _PUBLIC_URL | 61 return get_url_link(name, bucket, authenticated_link) |
64 return os.path.join(url_template % bucket, name) | 62 |
| 63 |
| 64 def upload_content_addressed( |
| 65 filepath, bucket, content_type=None, authenticated_link=True): |
| 66 """Uploads data to Google Storage with filename as sha1 hash. |
| 67 |
| 68 If file already exists in bucket with hash name, nothing is uploaded. |
| 69 """ |
| 70 sha1 = hashlib.sha1() |
| 71 with open(filepath, 'rb') as f: |
| 72 sha1.update(f.read()) |
| 73 sha1_hash = sha1.hexdigest() |
| 74 if not exists(sha1_hash, bucket): |
| 75 upload(sha1_hash, filepath, bucket, content_type, authenticated_link) |
| 76 return get_url_link(sha1_hash, bucket, authenticated_link) |
| 77 |
| 78 |
| 79 @decorators.NoRaiseException(default_return_value=False) |
| 80 def exists(name, bucket): |
| 81 bucket = _format_bucket_name(bucket) |
| 82 gs_path = 'gs://%s/%s' % (bucket, name) |
| 83 |
| 84 cmd = [_GSUTIL_PATH, '-q', 'stat', gs_path] |
| 85 return_code = cmd_helper.RunCmd(cmd) |
| 86 if return_code == 0: |
| 87 return True |
| 88 else: |
| 89 return False |
65 | 90 |
66 | 91 |
67 def unique_name(basename, suffix='', timestamp=True, device=None): | 92 def unique_name(basename, suffix='', timestamp=True, device=None): |
68 """Helper function for creating a unique name for a file to store in GS. | 93 """Helper function for creating a unique name for a file to store in GS. |
69 | 94 |
70 Args: | 95 Args: |
71 basename: Base of the unique filename. | 96 basename: Base of the unique filename. |
72 suffix: Suffix of filename. | 97 suffix: Suffix of filename. |
73 timestamp: Whether or not to add a timestamp to name. | 98 timestamp: Whether or not to add a timestamp to name. |
74 device: Device to add device serial of to name. | 99 device: Device to add device serial of to name. |
(...skipping 12 matching lines...) Expand all Loading... |
87 Args: | 112 Args: |
88 name: Name of the file on Google Storage. | 113 name: Name of the file on Google Storage. |
89 bucket: Bucket to upload file to. | 114 bucket: Bucket to upload file to. |
90 authenticated_link: Whether to return a link that requires user to | 115 authenticated_link: Whether to return a link that requires user to |
91 authenticate with a Google account. Setting this to false will return | 116 authenticate with a Google account. Setting this to false will return |
92 a link that does not require user to be signed into Google account but | 117 a link that does not require user to be signed into Google account but |
93 will only work for completely public storage buckets. | 118 will only work for completely public storage buckets. |
94 Returns: | 119 Returns: |
95 Web link to item to be uploaded to Google Storage bucket | 120 Web link to item to be uploaded to Google Storage bucket |
96 """ | 121 """ |
| 122 bucket = _format_bucket_name(bucket) |
| 123 url_template = _AUTHENTICATED_URL if authenticated_link else _PUBLIC_URL |
| 124 return os.path.join(url_template % bucket, name) |
| 125 |
| 126 |
| 127 def _format_bucket_name(bucket): |
97 if bucket.startswith('gs://'): | 128 if bucket.startswith('gs://'): |
98 bucket = bucket[len('gs://'):] | 129 bucket = bucket[len('gs://'):] |
99 if bucket.endswith('/'): | 130 if bucket.endswith('/'): |
100 bucket = bucket[:-1] | 131 bucket = bucket[:-1] |
101 url_template = _AUTHENTICATED_URL if authenticated_link else _PUBLIC_URL | 132 return bucket |
102 return os.path.join(url_template % bucket, name) | |
OLD | NEW |