2016-11-09 57 views
0

我正在试图从Google Storage下载一些文件,使用Python API。显然这可以通过使用gsutil's cp来完成,事实上我可以做到。如何通过python从Google Storage以编程方式下载文件?

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

然而,我发现不涉及此主题的所有Google Python API例子,我即使这个功能是由API覆盖的考虑。

+0

的gsutil是用Python编写;你几乎可以肯定地看到里面,看看它做什么。虽然我实际上认为它是博托周围的包装。 –

回答

2

(在谷歌第一的 “谷歌存储Python包装”)覆盖Python Example  |  Cloud Storage Documentation  |  Google Cloud Platform

要下载的对象: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 
+0

顺便说一下,我在[本网站](https://support.google.com/googleplay/android-developer/answer/6135870?p=stats_export&rd=1#export)上看了一眼,他们使用' json'但这个例子不起作用,因为'oauth2client.client.SignedJwtAssertionCredentials'和'apiclient.discovery import build'找不到,可能这种方法不再被支持吗?我想避免使用'gcloud init' –

+0

@AlbertoBonsanto该链接似乎是关于下载一些报告而不是用户文件。 –

+0

确实我想下载这些报告 –