繁体   English   中英

python上传文件到与我共享的google drive文件夹

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM