首页 > 解决方案 > Python - 将文件上传到 Google 团队云端硬盘

问题描述

我正在尝试通过 Python API 将文件上传到特定的团队驱动器,但正在努力指定在哪里指定 teamDriveId。我正在使用 Drive v3。

这是我正在使用的功能:

def upload_file(self):
    # self.service: Google Drive Hook - works well for other functions
    # self.drive_id: Id of Team Drive I am trying to upload to

    metadata = {'name': 'sample.txt'}
    media = MediaFileUpload('sample.txt', mimetype = 'text/plain')
    upload = self.service.files().create(body=metadata, 
                                         supportsTeamDrives=True,
                                         media_body=media,
                                         fields='id).execute()

我试图把它放在create()函数和metadataJSON 中,{'parents': self.drive_id}但这要么返回一个,Unexpected keyword argument: teamDriveId要么文件只会上传到我的个人驱动器。

我尝试使用的团队云端硬盘中没有任何文件夹。我看起来可以设置{parents': <teamDrive_folderId>},但我希望找到一个解决方案,我不必指定文件夹,只需将文件放在团队驱动器的根目录中即可。

任何建议将不胜感激。

标签: pythongoogle-drive-api

解决方案


设置parents为团队驱动器的 ID 对我有用,并将文件插入到团队驱动器的根目录中。最小的例子:

# media is a `MediaFileUpload` instance
service.files().create(
    supportsTeamDrives=True,
    media_body=media,
    body={
        'parents': ['0AILoX...'],  # ID of the Team Drive
        'name': 'foo.txt'
    }
).execute()

你确定你得到了正确的身份证?


推荐阅读