Alberto Bonsanto Alberto Bonsanto - 1 year ago 115
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 Source

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

To download an object: storage/api/ (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.
    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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download