簡體   English   中英

Google 雲端硬盤更新文件

[英]Google Drive Update File

from googleapiclient.http import MediaFileUpload
from Google import Create_Service

CLIENT_SECRET_FILE = 'client-secret.json'
API_NAME ='drive'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/drive.file']

service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

#Upload file
file_metadata = {
    'name': 'image.jpg', # Name to upload to Google Drive
    'parents': ['insert parents here']
}

media_content = MediaFileUpload('image.jpg', mimetype='image/jpg') # image from 
folder

file = service.files().create(
    body = file_metadata,
    media_body = media_content 
).execute()

print(file)

新手,有誰知道如何覆蓋上傳到 Google Drive 的單個文件,而無需使用文件 ID 手動替換文件? 另外,為什么Google Drive在通過上面的代碼上傳文件時不會自動覆蓋同名文件,就像手動上傳一樣? 它不斷在 Google 雲端硬盤中創建同名文件。

如果要修改文件的元數據或內容,只需使用files.update ,類似於:

file = service.files().get(fileId=file_id).execute()
media_body = MediaFileUpload(new_filename, mimetype=new_mime_type, resumable=True)
updated_file = service.files().update(fileId=file_id, body=file, newRevision=new_revision, media_body=media_body).execute()

此外,請記住 Drive UI 與 Drive API不同,這意味着某些功能的工作方式可能略有不同。 Drive API 通過使用文件 ID 來工作,因此無論何時您想要修改文件,都需要指定fileId ,因此您正在觀察的行為。

參考

暫無
暫無

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

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