Skip to content

Instantly share code, notes, and snippets.

@prats0599
Created March 6, 2020 18:06
Show Gist options
  • Save prats0599/af65da6fe33abbc4380c103f2b24c889 to your computer and use it in GitHub Desktop.
Save prats0599/af65da6fe33abbc4380c103f2b24c889 to your computer and use it in GitHub Desktop.
Unzipping a file on google cloud console
Display the source blob
Display the rendered blob
Raw
""" Activate your vm instance and run this on a jupyter notebook """
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("planet_data", "intel-image-classification.zip") # if the file is gs://planet_data/intel-image-classification.zip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment