How to zip files on a Cloud Storage Bucket using Python?

Asked

Viewed 40 times

0

Good morning guys! I need to zip the files present in a particular Cloud Storage Pack. I have tried several alternatives, but so far unsuccessful. Please, could you help me with this question?

At the moment, I’m only able to return the name of the blobs present in Bucket, however, I need to generate a zip with all the files inside and write to Bucket itself.

Follows code:

from google.cloud import storage
import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="file.json"

def zip_in_bucket(bucketname):

    client = storage.Client()
    bucket = client.get_bucket(bucketname)
    object_generator = bucket.list_blobs()
    for files in object_generator:
        print(files.name)

Return:

file-1.csv
file-2.csv
file-3.csv
file-4.csv
file-5.csv

Thank you in advance!

  • I don’t know how you can do this straight from python. But one output is to open the files in your VM, zip each one of them and then run gsutil mv *zip gs://nome_bucket/. Much more inefficient, though

  • So Lucas, I need to do it via Python anyway, because the process will need to be automated via Cloud Function.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.