我已成功使用PyDrive将文件上传到google-drive-folder。但是,当要将文件上传到与我共享的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})
我收到的错误消息是: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。
我一直在寻找用PyDrive将文件上传到Teamdrives的手段,但徒劳无功。我在pydrive的github页面上看到,他们在大约8个月前添加了对teamdrives的支持。但是我找不到有关如何使用它的任何文档。有人可以建议我错了吗?
答案 0 :(得分:0)
要进行上传,请按照以下说明尝试制作一个名为“ settings.yaml”的文件并将其保存在您的工作目录中: https://pythonhosted.org/PyDrive/oauth.html
您将需要在client_secrets.json文件中找到的客户端ID和客户端密钥,在授权访问Google API之后,该文件也应位于您的目录中。
使用以下代码对其进行测试,以在团队驱动器的文件夹中创建一个文本文件:
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.