如果文件存在于特定文件夹中并且具有特定名称,我正在尝试更新它。在这种情况下,有问题的对象在团队驱动器中。我按照文档将q
参数组合到list
调用中,尝试切换回 v2 ......看来查询的组合完全正确。话虽如此,即使我看到目标文件夹中存在多个对象,列表调用也看不到它们。我试过name = ''
和name contains ''
。谷歌团队似乎已经进行了足够的输入验证,就像当我有创意的 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")
当我尝试将“名称”参数替换为“标题”(根据我查看的一些答案,用于在 v2 中工作)时,API 被拒绝了
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">