简体   繁体   English

使用PyDrive将文件上传到Google-Drive Teamdrive文件夹

[英]Upload File to Google-drive Teamdrive folder with PyDrive

I have been successfully uploading files to a google-drive-folder with PyDrive. 我已经使用PyDrive成功地将文件上传到了google-drive-folder。 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. 但是,将文件上传到与我共享的google-drive-teamdrive-folder文件夹中时,以下代码不起作用。

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})

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.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。

I have been searching for means to upload files to Teamdrives with PyDrive but in vain. 我一直在寻找用PyDrive将文件上传到Teamdrives的手段,但徒劳无功。 I see in the pydrive's github pages that they added the teamdrives support approx 8 month ago. 我在pydrive的github页面上看到,他们在大约8个月前添加了对teamdrives的支持。 But I cannot find any documentation on how to use that. 但是我找不到有关如何使用它的任何文档。 Can anyone suggest where I am being wrong please? 有人可以建议我错了吗?

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 对于上传,请尝试制作一个名为“ settings.yaml”的文件,然后按照此处的说明将其保存在您的工作目录中: https : //pythonhosted.org/PyDrive/oauth.html

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. 您将需要在client_secrets.json文件中找到的客户端ID和客户端密钥,在授权访问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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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