[英]python upload file to google drive folder that is shared with me
我正在使用Python 2.7,並且試圖將文件(* .txt)上傳到與我共享的文件夾中。
到目前為止,我已經能夠將其上傳到雲端硬盤,但如何設置到哪個文件夾。 我將URL轉到必須將此文件放置的位置。
謝謝
到目前為止,這是我的代碼
def Upload(file_name, file_path, upload_url):
upload_url = upload_url
client = gdata.docs.client.DocsClient(source=upload_url)
client.api_version = "3"
client.ssl = True
client.ClientLogin(username, passwd, client.source)
filePath = file_path
newResource = gdata.docs.data.Resource(filePath,file_name)
media = gdata.data.MediaSource()
media.SetFileHandle(filePath, 'mime/type')
newDocument = client.CreateResource(
newResource,
create_uri=gdata.docs.client.RESOURCE_UPLOAD_URI,
media=media
)
您使用的API已棄用。 請改用google-api-python-client 。
遵循此官方python快速入門指南 ,只需將文件上傳到文件夾即可。 此外,像這樣在請求正文中發送parents參數: body['parents'] = [{'id': parent_id}]
或者,您可以使用PyDrive (Python包裝器庫),該庫可以簡化許多處理Google Drive API的工作。 整個代碼很簡單:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
drive = GoogleDrive(gauth)
f = drive.CreateFile({'parent': parent_id})
f.SetContentFile('cat.png') # Read local file
f.Upload() # Upload it
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.