Alberto Bonsanto Alberto Bonsanto - 28 days ago 16
Python Question

How to download files programatically from Google Storage via python?

I'm currently trying to download some files from

Google Storage
using the Python API. Obviously this can be done by using gsutil's cp, and indeed I could do it.

sudo gsutil cp gs://???/stats/installs/*.csv .


However, all examples of
Google Python API
I've found don't cover this subject, I'm even considering if this functionality is covered by the API.

Answer

This is covered by Python Example  |  Cloud Storage Documentation  |  Google Cloud Platform:

To download an object: storage/api/crud_object.py (View on GitHub)

def get_object(bucket, filename, out_file):
    service = create_service()

    # Use get_media instead of get to get the actual contents of the object.
    # http://g.co/dv/resources/api-libraries/documentation/storage/v1/python/latest/storage_v1.objects.html#get_media
    req = service.objects().get_media(bucket=bucket, object=filename)

    downloader = http.MediaIoBaseDownload(out_file, req)

    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download {}%.".format(int(status.progress() * 100)))

    return out_file