gpt4 book ai didi

python - 使用Python通过API将一个文件上传到多文件夹谷歌驱动器

转载 作者:行者123 更新时间:2023-11-30 22:43:28 25 4
gpt4 key购买 nike

我想通过 API 将文件上传到多文件夹谷歌驱动程序,但只会保存一个文件,而不是每个文件夹的每个文件。 (多个文件夹中有 1 个文件)

手动使用示例:Add the Same File to Multiple Folders in Google Drive without Copying

你能帮帮我吗?谢谢!

最佳答案

inserting a file in a folder ,您需要在文件的 parents 属性中指定正确的文件夹 ID。使用Python:

folder_id = '0BwwA4oUTeiV1TGRPeTVjaWRDY1E'
file_metadata = {
'name' : 'photo.jpg',
'parents': [ folder_id ]
}
media = MediaFileUpload('files/photo.jpg',
mimetype='image/jpeg',
resumable=True)
file = drive_service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print 'File ID: %s' % file.get('id')

正如 Files: insert 中进一步提到的,在请求正文中设置 parents[] 属性会将文件放入所有提供的文件夹中。如果parents[]字段中没有提供文件夹,文件将被放置在默认根文件夹中。

希望有帮助!

关于python - 使用Python通过API将一个文件上传到多文件夹谷歌驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41760345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com