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

时间:2016-11-09 15:41:15

标签: python google-cloud-storage

我目前正在尝试使用Python APIGoogle Storage下载一些文件。显然,这可以通过使用gsutil's cp来完成,事实上我可以做到。

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

但是,我发现Google Python API的所有示例都没有涵盖这个主题,我甚至在考虑API是否涵盖了此功能。

1 个答案:

答案 0 :(得分:4)

涵盖Python Example  |  Cloud Storage Documentation  |  Google Cloud Platform(谷歌中的第一个“google storage python wrapper”):

  

下载对象: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
相关问题