python - 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()
函数和metadata
JSON 中,{'parents': self.drive_id}
但这要么返回一个,Unexpected keyword argument: teamDriveId
要么文件只会上传到我的个人驱动器。
我尝试使用的团队云端硬盘中没有任何文件夹。我看起来可以设置{parents': <teamDrive_folderId>}
,但我希望找到一个解决方案,我不必指定文件夹,只需将文件放在团队驱动器的根目录中即可。
任何建议将不胜感激。
解决方案
设置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()
你确定你得到了正确的身份证?
推荐阅读
- android - 在这种情况下,如何将 textView 中的文本居中?
- javascript - 遍历 javascript 对象以查看文本(如 python requests.text)
- html - 提前知道是否会触发重新验证码(隐形 V2、V3)
- amazon-web-services - AWS 固定性能实例和 AWS Burstable + Unlimited 模式实例之间的区别
- javascript - 当父元素具有属性指针事件时如何使子元素打开链接:无?
- android-studio - Flutter / Dart 错误:参数类型“Null Function(Key)”无法分配给参数类型“void Function(Object)”
- jquery - 发送电子邮件而不刷新页面 PHP AJAX。email.php 发送电子邮件操作是 HTML
- xaml - 在屏幕中央放置一个stacklayout
- android - 带有过滤器的 Kotlin For 循环仅产生 1 个结果
- c# - 为数据库中新添加的列更新数据,代码优先