2016-11-09 59 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

確實我想下載這些報告 –