gcs_download.py (762B)
1 # Copyright 2022 The Chromium Authors 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import os 6 import sys 7 import tarfile 8 import tempfile 9 10 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), 11 'test'))) 12 from gs_util_wrapper import run_gsutil 13 14 def DownloadAndUnpackFromCloudStorage(url, output_dir): 15 """Fetches a tarball from GCS and uncompresses it to |output_dir|.""" 16 17 tmp_file = 'image.tgz' 18 with tempfile.TemporaryDirectory() as tmp_d: 19 tmp_file_location = os.path.join(tmp_d, tmp_file) 20 run_gsutil(['cp', url, tmp_file_location]) 21 tarfile.open(name=tmp_file_location, 22 mode='r|gz').extractall(path=output_dir)