使用python通过API将一个文件上传到多文件夹google驱动程序

时间:2017-01-20 09:38:44

标签: python google-drive-api

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

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

你能帮帮我吧!谢谢!

1 个答案:

答案 0 :(得分:3)

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[]字段中未提供文件夹,则该文件将放在默认的根文件夹中。

希望有所帮助!