如果存在相同名称的对象,Google Drive API v3会更新一个对象:list'q'参数是否无法按文档所述工作?

时间:2018-09-28 04:04:06

标签: google-drive-api google-drive-team-drive

如果文件存在于特定文件夹中并且具有特定名称,我正在尝试更新它。在这种情况下,所讨论的对象在团队合作中。我按照文档将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倒车了

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">

1 个答案:

答案 0 :(得分:1)

感谢@tehhowch

当需要设置团队目标时,需要采取额外的措施,即includeTeamDriveItems选项,否则默认情况下不包括TD位置:

start