0

如果文件存在于特定文件夹中并且具有特定名称,我正在尝试更新它。在这种情况下,有问题的对象在团队驱动器中。我按照文档将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">
4

1 回答 1

1

谢谢@tehhowch,

当需要设置团队驱动中的目标时,确实需要额外的措施,即 includeTeamDriveItems 选项,否则默认不包含 TD 位置:

   check_if_already_exists = DRIVE.files().list(
       q=query, 
       fields="files(id, name)",
       supportsTeamDrives=True,
       includeTeamDriveItems=True
   ).execute()
于 2018-09-29T14:55:54.023 回答