使用 PyDrive 将文件上传到 Google-drive Teamdrive 文件夹
Upload File to Google-drive Teamdrive folder with PyDrive
我已经使用 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' 是此处的 teamdrive 文件夹 ID。
我一直在寻找使用 PyDrive 将文件上传到 Teamdrives 的方法,但没有成功。我在 pydrive 的 github 页面中看到他们在大约 8 个月前添加了 teamdrives 支持。但是我找不到任何关于如何使用它的文档。谁能指出我错在哪里?
要上传,请尝试制作一个名为 "settings.yaml" 的文件并将其保存在您的工作目录中,按照此处的说明:
https://pythonhosted.org/PyDrive/oauth.html
您将需要在 client_secrets.json 文件中找到的客户端 ID 和客户端密码,在您授权访问 Google API.[=12 之后,该文件也应该在您的目录中=]
使用以下代码进行测试,在团队驱动器的文件夹中创建一个文本文件:
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-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' 是此处的 teamdrive 文件夹 ID。
我一直在寻找使用 PyDrive 将文件上传到 Teamdrives 的方法,但没有成功。我在 pydrive 的 github 页面中看到他们在大约 8 个月前添加了 teamdrives 支持。但是我找不到任何关于如何使用它的文档。谁能指出我错在哪里?
要上传,请尝试制作一个名为 "settings.yaml" 的文件并将其保存在您的工作目录中,按照此处的说明: https://pythonhosted.org/PyDrive/oauth.html
您将需要在 client_secrets.json 文件中找到的客户端 ID 和客户端密码,在您授权访问 Google API.[=12 之后,该文件也应该在您的目录中=]
使用以下代码进行测试,在团队驱动器的文件夹中创建一个文本文件:
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.