1

In this code

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

how to add the project and auth - @sylvain-gantois implied it can be done. Any help would be awesome.

  • Welcome to SO. Please have a look as how to ask a good question here: [ask] You can also take a [tour] to know more about how SO is working. Then give more details, and a minimal reproducible example with code you wrote yet / sample data / and full error messages about your trial to solve this problem, so we can reproduce and help. See [MRE] – Malo Jul 10 '22 at 19:48

1 Answers1

0

All - I worked it out

data = {CONTENTS OF GOOGLE IAM JSON}

with open('google.json', 'w') as outfile:
    json.dump(data, outfile, ensure_ascii=False)

google_credentials = service_account.Credentials.from_service_account_file('google.json')
storage_client = storage.Client(project='PROJECTID', credentials=google_credentials)
AlexK
  • 2,855
  • 9
  • 16
  • 27