Alberto Bonsanto
Alberto Bonsanto

Reputation: 18022

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.

Upvotes: 2

Views: 4414

Answers (1)

ivan_pozdeev
ivan_pozdeev

Reputation: 35986

This is covered by Python Example  |  Cloud Storage Documentation  |  Google Cloud Platform (the 1st in Google on "google storage python wrapper"):

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

Upvotes: 4

Related Questions