本文介绍了使用PyDrive将文件上传到Google-Drive Teamdrive文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用PyDrive成功地将文件上传到了google-drive-folder.但是,将文件上传到与我共享的google-drive-teamdrive-folder文件夹中时,以下代码不起作用.

I have been successfully uploading files to a google-drive-folder with PyDrive. But, when it comes to uploading files to a folder in a google-drive-teamdrive-folder which is shared with me, the following code is not working.

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"是团队驱动器的文件夹ID.

The error message I am recieving is: 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' is the teamdrive's folder id here.

我一直在寻找用PyDrive将文件上传到Teamdrives的手段,但徒劳无功.我在pydrive的github页面上看到,他们在大约8个月前添加了对teamdrives的支持.但是我找不到任何有关如何使用它的文档.有人可以建议我错了吗?

I have been searching for means to upload files to Teamdrives with PyDrive but in vain. I see in the pydrive's github pages that they added the teamdrives support approx 8 month ago. But I cannot find any documentation on how to use that. Can anyone suggest where I am being wrong please?

推荐答案

要进行上传,请尝试制作一个名为"settings.yaml"的文件,然后按照此处的说明将其保存在您的工作目录中: https://pythonhosted.org/PyDrive/oauth.html

For uploading, try making a file called "settings.yaml" and saving it in your working directory, as per the instructions here:https://pythonhosted.org/PyDrive/oauth.html

您将需要在client_secrets.json文件中找到的客户端ID和客户端密钥,在授权访问Google API之后,该文件也应位于您的目录中.

You will need the client id and client secret found in the client_secrets.json file which should also be in your directory after you authorised access to the Google API.

使用以下代码对其进行测试,以在团队驱动器的文件夹中创建一个文本文件:

Test it out with the following code to make a text file in a folder in the 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.

这篇关于使用PyDrive将文件上传到Google-Drive Teamdrive文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:24