def FetchFromCloudStorage(bucket_name, source_path, destination_dir): """Fetches file(s) from the Google Cloud Storage. As a side-effect, this prints messages to stdout about what's happening. Args: bucket_name: Google Storage bucket name. source_path: Source file path. destination_dir: Destination file path. Returns: Local file path of downloaded file if it was downloaded. If the file does not exist in the given bucket, or if there was an error while downloading, None is returned. """ target_file = os.path.join(destination_dir, os.path.basename(source_path)) gs_url = 'gs://%s/%s' % (bucket_name, source_path) try: if cloud_storage.Exists(bucket_name, source_path): logging.info('Fetching file from %s...', gs_url) cloud_storage.Get(bucket_name, source_path, target_file) if os.path.exists(target_file): return target_file else: logging.info('File %s not found in cloud storage.', gs_url) return None except Exception as e: logging.warn('Exception while fetching from cloud storage: %s', e) if os.path.exists(target_file): os.remove(target_file) return None
def _DownloadFromCloudStorage(self, img_name, page, tab): """Downloads the reference image for the given test from cloud storage, returning it as a Telemetry Bitmap object.""" # TODO(kbr): there's a race condition between the deletion of the # temporary file and gsutil's overwriting it. if not self.options.refimg_cloud_storage_bucket: raise Exception('--refimg-cloud-storage-bucket argument is required') temp_file = tempfile.NamedTemporaryFile().name cloud_storage.Get(self.options.refimg_cloud_storage_bucket, self._FormatReferenceImageName(img_name, page, tab), temp_file) return bitmap.Bitmap.FromPngFile(temp_file)