[英]python upload file to google drive folder that is shared with me
I am using Python 2.7 and I am trying to upload a file (*.txt) into a folder that is shared with me. 我正在使用Python 2.7,并且试图将文件(* .txt)上传到与我共享的文件夹中。
So far I was able to upload it to my Drive, but how to set to which folder. 到目前为止,我已经能够将其上传到云端硬盘,但如何设置到哪个文件夹。 I get the url to where I must place this file.
我将URL转到必须将此文件放置的位置。
Thank you 谢谢
this is my code so far 到目前为止,这是我的代码
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
)
the API you are using is deprecated. 您使用的API已弃用。 Use google-api-python-client instead.
请改用google-api-python-client 。
Follow this official python quickstart guide to simply upload a file to a folder. 遵循此官方python快速入门指南 ,只需将文件上传到文件夹即可。 Additionally, send parents parameter in request body like this:
body['parents'] = [{'id': parent_id}]
此外,像这样在请求正文中发送parents参数:
body['parents'] = [{'id': parent_id}]
Or, you can use PyDrive , a Python wrapper library which simplifies a lot of works dealing with Google Drive API. 或者,您可以使用PyDrive (Python包装器库),该库可以简化许多处理Google Drive API的工作。 The whole code is as simple as this:
整个代码很简单:
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.