如何通过 python 从 Google 存储以编程方式下载文件?

How to download files programatically from Google Storage via python?

我目前正在尝试使用 Python API. Obviously this can be done by using gsutil's cpGoogle Storage 下载一些文件,我确实可以做到。

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

但是,我发现的 Google Python API 的所有示例都没有涵盖这个主题,我什至在考虑 API.

是否涵盖了此功能

这个 Python Example  |  Cloud Storage Documentation  |  Google Cloud Platform 覆盖("google storage python wrapper" 上 Google 中的第一个):

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