I modified the code from this thread to unzip a .gz file, but I'm getting the following error:
How do I unzip a .zip file in google cloud storage?
AttributeError: 'GzipFile' object has no attribute 'namelist'
My code is as follows:
from google.cloud import storage
from gzip import GzipFile
import io
def gzipextract(bucketname, gzipfilename_with_path):
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucketname)
destination_blob_pathname = gzipfilename_with_path
blob = bucket.blob(destination_blob_pathname)
gzipbytes = io.BytesIO(blob.download_as_string())
with GzipFile(fileobj = gzipbytes, mode = 'r') as mygzip:
for contentfilename in mygzip.namelist():
contentfile = mygzip.read(contentfilename)
blob = bucket.blob(gzipfilename_with_path + "/" + contentfilename)
blob.upload_from_string(contentfile)