Python-将文件上传到Google Team Drive [英] Python - Upload file to Google Team Drive

查看:181
本文介绍了Python-将文件上传到Google Team Drive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Python API将文件上传到特定的Team Drive,但在指定teamDriveId的位置上很费劲.我正在使用Drive v3.

I'm trying to upload a file to a specific Team Drive via the Python API, but am struggling with where to specify the teamDriveId. I'm working with Drive v3.

这是我正在使用的功能:

Here is the function I am working with:

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()

我试图将其作为{'parents': self.drive_id}放在create()函数和metadata JSON中,但这将返回Unexpected keyword argument: teamDriveId或文件将仅上传到我的个人驱动器.

I have attempted to put this in both the create() function and the metadata JSON as {'parents': self.drive_id} but this either return an Unexpected keyword argument: teamDriveId or the file will just upload to my personal drive.

我要使用的Team Drive内没有任何文件夹.我似乎可以设置{parents': <teamDrive_folderId>},但是我希望找到一种无需指定文件夹的解决方案,而只需将文件放在Team Drive的根目录中即可.

I do not have any folders inside the Team Drive I am attempting to work with. I looks like I could set {parents': <teamDrive_folderId>} but I am hoping to find a solution where I don't have to specify a folder and can just put files in the root of the Team Drive.

任何建议将不胜感激.

推荐答案

parents设置为Team Drive的ID对我有用,并将文件插入Team Drive的根目录.最小的例子:

Setting parents to the Team Drive's ID works for me and inserts the file into the root of the Team Drive. Minimal example:

# 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()

您确定自己的ID正确吗?

Are you sure that you got the right ID?

这篇关于Python-将文件上传到Google Team Drive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆