簡體   English   中英

Google Drive API v3在python中隨時隨地更新文件(使用實時數據)

[英]Google Drive API v3 updating a file on the go (using realtime data) in python

我正在嘗試使用一些新數據更新我創建的文件。 本質上,我試圖將內容附加到已創建的文件。 我嘗試過使用不同屬性的不同方法,但到目前為止似乎沒有任何工作。 從v2到v3的遷移似乎使python中的開發變得更加困難。

到目前為止這是我的代碼

def updateFile(fileID, contents):
    credentials = get_credentials() #this function gets the credentials for oauth
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)
    # First retrieve the file from the API.

    #fileCreated = service.files().get(fileId=fileID).execute()
    #print(dir(fileCreated.update()))
    # File's new content.
    file_metadata = { 'name' : 'notes.txt', 'description' : 'this is a test' }
    fh  = BytesIO(contents.encode())
    media = MediaIoBaseUpload(fh,
                        mimetype='text/plain')
    # Send the request to the API.
    #print(BytesIO(contents.encode()).read())
    print(fileID)
    updated_file = service.files().update(
        body=file_metadata,
        #uploadType = 'media',
        fileId=fileID,
        #fields = fileID,
        media_body=media).execute()

我嘗試過使用MediaFileUpload(它僅用於上傳文件),但我附加了一個“字符串”,它是使用語音界面實時生成的,不會存儲在任何文件中。 所以我最終使用了MediaIoBaseUpload。

代碼運行時沒有任何錯誤,但文件未更新。 任何幫助表示贊賞。

好的,所以我弄清楚我的代碼出了什么問題。 顯然,我發布的這一大塊代碼沒有任何問題。 我意識到我沒有將我創建的文檔轉換為“google文檔”,因此代碼沒有更新文檔。 我將我創建的文檔的mime類型更改為google文檔,現在代碼工作正常:)

您還可以使用service.files().get_media(fileId=fileID).execute()來獲取文件內容並向其添加新內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM