| Index: build/android/pylib/utils/google_storage_helper.py
|
| diff --git a/build/android/pylib/utils/google_storage_helper.py b/build/android/pylib/utils/google_storage_helper.py
|
| index 7a993a162f9d3290a9f61bf93864096c97e2373f..a9130734048b77017c3087447cc4b217736fbc81 100644
|
| --- a/build/android/pylib/utils/google_storage_helper.py
|
| +++ b/build/android/pylib/utils/google_storage_helper.py
|
| @@ -9,6 +9,7 @@ Due to logdog not having image or HTML viewer, those instead should be uploaded
|
| to Google Storage directly using this module.
|
| """
|
|
|
| +import hashlib
|
| import logging
|
| import os
|
| import sys
|
| @@ -45,10 +46,7 @@ def upload(name, filepath, bucket, content_type=None, authenticated_link=True):
|
| Returns:
|
| Web link to item uploaded to Google Storage bucket.
|
| """
|
| - if bucket.startswith('gs://'):
|
| - bucket = bucket[len('gs://'):]
|
| - if bucket.endswith('/'):
|
| - bucket = bucket[:-1]
|
| + bucket = _format_bucket_name(bucket)
|
|
|
| gs_path = 'gs://%s/%s' % (bucket, name)
|
| logging.info('Uploading %s to %s', filepath, gs_path)
|
| @@ -60,8 +58,35 @@ def upload(name, filepath, bucket, content_type=None, authenticated_link=True):
|
|
|
| cmd_helper.RunCmd(cmd)
|
|
|
| - url_template = _AUTHENTICATED_URL if authenticated_link else _PUBLIC_URL
|
| - return os.path.join(url_template % bucket, name)
|
| + return get_url_link(name, bucket, authenticated_link)
|
| +
|
| +
|
| +def upload_content_addressed(
|
| + filepath, bucket, content_type=None, authenticated_link=True):
|
| + """Uploads data to Google Storage with filename as sha1 hash.
|
| +
|
| + If file already exists in bucket with hash name, nothing is uploaded.
|
| + """
|
| + sha1 = hashlib.sha1()
|
| + with open(filepath, 'rb') as f:
|
| + sha1.update(f.read())
|
| + sha1_hash = sha1.hexdigest()
|
| + if not exists(sha1_hash, bucket):
|
| + upload(sha1_hash, filepath, bucket, content_type, authenticated_link)
|
| + return get_url_link(sha1_hash, bucket, authenticated_link)
|
| +
|
| +
|
| +@decorators.NoRaiseException(default_return_value=False)
|
| +def exists(name, bucket):
|
| + bucket = _format_bucket_name(bucket)
|
| + gs_path = 'gs://%s/%s' % (bucket, name)
|
| +
|
| + cmd = [_GSUTIL_PATH, '-q', 'stat', gs_path]
|
| + return_code = cmd_helper.RunCmd(cmd)
|
| + if return_code == 0:
|
| + return True
|
| + else:
|
| + return False
|
|
|
|
|
| def unique_name(basename, suffix='', timestamp=True, device=None):
|
| @@ -94,9 +119,14 @@ def get_url_link(name, bucket, authenticated_link=True):
|
| Returns:
|
| Web link to item to be uploaded to Google Storage bucket
|
| """
|
| + bucket = _format_bucket_name(bucket)
|
| + url_template = _AUTHENTICATED_URL if authenticated_link else _PUBLIC_URL
|
| + return os.path.join(url_template % bucket, name)
|
| +
|
| +
|
| +def _format_bucket_name(bucket):
|
| if bucket.startswith('gs://'):
|
| bucket = bucket[len('gs://'):]
|
| if bucket.endswith('/'):
|
| bucket = bucket[:-1]
|
| - url_template = _AUTHENTICATED_URL if authenticated_link else _PUBLIC_URL
|
| - return os.path.join(url_template % bucket, name)
|
| + return bucket
|
|
|