用PyDri将文件上传到Googledrive Teamdrive文件夹

2024-09-30 08:33:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经成功地用PyDrive将文件上传到google驱动器文件夹中。但是,当要将文件上传到与我共享的googledrive teamdrive文件夹中的一个文件夹时,以下代码不起作用。在

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)


location_to_save = "D:\images"
mImageLoc =  location_to_save + "\\abcd.jpg"

#[...Code to fetch and save the file as abcd.jpg ...]

gfolder_id = "1H1gjBKcpiHJtnXKVxWQEC1CS8t4Gswjj"  #This is a google drive folder id. I am replacing this with a teamdrive folder id, but that does not work
gfile_title = mImageLoc.split("\\")[-1] # returns abcd.jpg

http = gdrive.auth.Get_Http_Object()
f = gdrive.CreateFile({"parents": [{"kind": "drive#fileLink", "id": gfolder_id}],
                                   'title': gfile_title})
            f.SetContentFile(mImageLoc)
            f.Upload(param={"http": http})

我收到的错误消息是:pydrive.files.ApiRequestError: <HttpError 404 when requesting https://www.googleapis.com/upload/drive/v2/files?alt=json&uploadType=resumable returned "File not found: 0AG-N4DqGC1nbUk9PVA"> '0AG-N4DqGC1nbUk9PVA'是teamdrive的文件夹id。在

我一直在寻找用PyDrive上传文件到Teamdrives的方法,但是没有成功。我在pydrive的github页面上看到,他们在大约8个月前添加了teamdrives支持。但是我找不到任何关于如何使用它的文档。谁能告诉我哪里错了吗?在


Tags: 文件to文件夹idhttptitlesavedrive
1条回答
网友
1楼 · 发布于 2024-09-30 08:33:06

要上载,请尝试制作一个名为“的文件”设置.yaml“并按照以下说明将其保存在您的工作目录中: https://pythonhosted.org/PyDrive/oauth.html

您需要在客户端中找到的客户端id和客户端密码_机密.json在你授权访问googleapi之后,这个文件也应该在你的目录中。在

使用以下代码进行测试,在team drive中的文件夹中生成一个文本文件:

parent_folder_id = 'YYYY'

f = drive.CreateFile({
    'title': 'test.txt',
    'parents': [{
        'kind': 'drive#fileLink',
        'teamDriveId': team_drive_id,
        'id': parent_folder_id
    }]
})
f.SetContentString('Hello World')

f.Upload(param={'supportsTeamDrives': True})

# where XXXX and YYYY are the team drive and target folder ids found from the end of the URLS when you open them in your browser.

相关问题 更多 >

    热门问题