Google 驱动器 API v3 如果存在同名对象则更新一个对象:列表 'q' 参数没有按记录工作?
Google Drive API v3 update an object if one exists with same name: list 'q' parameter does not work as documented?
如果文件存在于特定文件夹中并具有特定名称,我正在尝试更新该文件。在这种情况下,所讨论的对象位于团队驱动器中。我按照文档将 q
参数组合到 list
调用中,尝试切换回 v2...看起来查询的组合完全正确。也就是说,即使我看到目标文件夹中存在多个对象,列表调用也看不到它们。我试过 name = ''
和 name contains ''
。 google 团队似乎进行了足够的输入验证,当我对 API 炸弹产生创意时。有什么指点吗?
def import_or_replace_csv_to_td_folder(self, folder_id, local_fn, remote_fn, mime_type):
DRIVE = build('drive', 'v3', http=creds.authorize(Http()))
query = "'{0}' in parents and name = '{1}'.format(folder_id, remote_fn)
print("Searching for previous versions of this file : {0}".format(query))
check_if_already_exists = DRIVE.files().list(q=query, fields="files(id, name)").execute()
name_and_location_conflict = check_if_already_exists.get('files', [])
if not name_and_location_conflict:
body = {'name': remote_fn, 'mimeType': mime_type, 'parents': [folder_id]}
out = DRIVE.files().create(body=body, media_body=local_fn, supportsTeamDrives=True, fields='id').execute().get('id')
return out
else:
if len(name_and_location_conflict)==1:
file_id=name_and_location_conflict['id']
DRIVE.files().update(fileId=file_id, supportsTeamDrives=True, media_body=local_fn)
return file_id
else:
raise MultipleConflictsError("There are multiple documents matching parent folder and file name. Unclear which requires a version update")
当我尝试将 'name' 参数替换为 'title' 时(根据我查看的一些答案,以前在 v2 中工作)API barfed
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files?q=%27xxxxxxxxxxxxxxxx%27+in+parents+and+title+%3D+%27Somefile_2018-09-27.csv%27&fields=files%28id%2C+name%29&alt=json returned "Invalid Value">
感谢@tehhowch,
当目标在团队驱动中时确实需要额外的措施,即需要设置includeTeamDriveItems选项,否则默认不包括TD位置:
check_if_already_exists = DRIVE.files().list(
q=query,
fields="files(id, name)",
supportsTeamDrives=True,
includeTeamDriveItems=True
).execute()
如果文件存在于特定文件夹中并具有特定名称,我正在尝试更新该文件。在这种情况下,所讨论的对象位于团队驱动器中。我按照文档将 q
参数组合到 list
调用中,尝试切换回 v2...看起来查询的组合完全正确。也就是说,即使我看到目标文件夹中存在多个对象,列表调用也看不到它们。我试过 name = ''
和 name contains ''
。 google 团队似乎进行了足够的输入验证,当我对 API 炸弹产生创意时。有什么指点吗?
def import_or_replace_csv_to_td_folder(self, folder_id, local_fn, remote_fn, mime_type):
DRIVE = build('drive', 'v3', http=creds.authorize(Http()))
query = "'{0}' in parents and name = '{1}'.format(folder_id, remote_fn)
print("Searching for previous versions of this file : {0}".format(query))
check_if_already_exists = DRIVE.files().list(q=query, fields="files(id, name)").execute()
name_and_location_conflict = check_if_already_exists.get('files', [])
if not name_and_location_conflict:
body = {'name': remote_fn, 'mimeType': mime_type, 'parents': [folder_id]}
out = DRIVE.files().create(body=body, media_body=local_fn, supportsTeamDrives=True, fields='id').execute().get('id')
return out
else:
if len(name_and_location_conflict)==1:
file_id=name_and_location_conflict['id']
DRIVE.files().update(fileId=file_id, supportsTeamDrives=True, media_body=local_fn)
return file_id
else:
raise MultipleConflictsError("There are multiple documents matching parent folder and file name. Unclear which requires a version update")
当我尝试将 'name' 参数替换为 'title' 时(根据我查看的一些答案,以前在 v2 中工作)API barfed
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files?q=%27xxxxxxxxxxxxxxxx%27+in+parents+and+title+%3D+%27Somefile_2018-09-27.csv%27&fields=files%28id%2C+name%29&alt=json returned "Invalid Value">
感谢@tehhowch,
当目标在团队驱动中时确实需要额外的措施,即需要设置includeTeamDriveItems选项,否则默认不包括TD位置:
check_if_already_exists = DRIVE.files().list(
q=query,
fields="files(id, name)",
supportsTeamDrives=True,
includeTeamDriveItems=True
).execute()