Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unzip a .zip file in google cloud storage?

How do I unzip a .zip file in Goolge Cloud Storage Bucket? (If we have some other tool like 'CloudBerry Explorer' for AWS, that will be great.)

like image 916
Naaga A Avatar asked Mar 28 '18 17:03

Naaga A


People also ask

Can you unzip files in Google Cloud Storage?

There are Data flow templates in google Cloud data flow which helps to Zip/unzip the files in cloud storage.

How do I unzip a cloud shell in GCP?

1) Copy the file from Cloud Storage to the Cloud Shell file system using gsutil. 2) Unzip the downloaded file. 3) The program unzip does not contain drivers to support the Cloud Storage gs: namespace.


1 Answers

You can use Python, e.g. from a Cloud Function:

    from google.cloud import storage     from zipfile import ZipFile     from zipfile import is_zipfile     import io      def zipextract(bucketname, zipfilename_with_path):          storage_client = storage.Client()         bucket = storage_client.get_bucket(bucketname)          destination_blob_pathname = zipfilename_with_path                  blob = bucket.blob(destination_blob_pathname)         zipbytes = io.BytesIO(blob.download_as_string())          if is_zipfile(zipbytes):             with ZipFile(zipbytes, 'r') as myzip:                 for contentfilename in myzip.namelist():                     contentfile = myzip.read(contentfilename)                     blob = bucket.blob(zipfilename_with_path + "/" + contentfilename)                     blob.upload_from_string(contentfile)      zipextract("mybucket", "path/file.zip") # if the file is gs://mybucket/path/file.zip 
like image 109
Daniel Sparing Avatar answered Sep 17 '22 14:09

Daniel Sparing