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

标签 python google-cloud-storage

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

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

但是,我发现的所有 Google Python API 示例均未涵盖此主题,我什至正在考虑 API 是否涵盖此功能。

最佳答案

Python Example  |  Cloud Storage Documentation  |  Google Cloud Platform 涵盖(Google 中关于“Google Storage Python 包装器”的第 1 个):

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

关于python - 如何通过 python 以编程方式从 Google Storage 下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40510782/

相关文章:

python - 如何使用Python mechanize保存页面,包括页面上的图像

python - 是否可以引用未知数量的变量?

Python Tkinter 菜单栏

go - 无法使用默认服务帐户和谷歌云库从谷歌 Kubernetes 引擎访问谷歌云存储

google-cloud-storage - 使用访问限制从 GCS 提供静态页面

Django,Heroku,boto : direct file upload to Google cloud storage

python - Pandas 的 Convert_dtypes 不适用于标记为对象的数字

python - "Registration timeout"在非常基本的 Python IRC 机器人上

java - 谷歌云存储 : Could not initialize class OauthRawGcsServiceFactory

google-cloud-storage - 删除文件夹 Google Cloud Storage 下的所有文件