Copy file into a specific parent folder with Google Drive API?

时光总嘲笑我的痴心妄想 提交于 2019-12-07 06:13:55

问题


I'm using Python 2.7 and a service account to copy a file in Google Drive into another folder based on its title. This requires me to execute five (5!) commands:

  1. Find file ID by title. (files().list)
  2. Find parent folder ID by title (files().list)
  3. Copy file (files().copy)
  4. Transfer ownership to a real account (files().insert)
  5. Move into the parent folder. (parents().insert)

This all works but I'd like to reduce the number of calls and first that means caching the ID's so I don't have to call files().list. The next thing I'm trying to do, and specifically where I'm at with this question, is how to set the parent folder within the files().copy command. The documentation has an optional parents parameter described like so:

Collection of parent folders which contain this file. Setting this field will put the file in all of the provided folders. On insert, if no folders are provided, the file will be placed in the default root folder.

Although it doesn't say, I know it means parent ID's since that's what is used everywhere else. However, setting this array in the client library doesn't produce an effect: no error and the file is definitely not in the right folder.

newfile = {'title': newtitle, 'parents' : [ parentFolderId ]}
service.files().copy(fileId=originalId, body=newfile).execute()

Has anyone had any luck with this? Is there something else I'm missing?

Bonus: transfer ownership in copy command? Perhaps my service account could impersonate me?


回答1:


Ah ha! The parents array is a list of objects with the id field:

newfile = {'title': newtitle, 'parents' : [ { "id" : parentFolderId } ]}
service.files().copy(fileId=originalId, body=newfile).execute()

I'll update this if/when I figure out how to set permissions also.

One strange note here is that the file is still being copied to the drive root as well as the parent folder(s) I specify.



来源:https://stackoverflow.com/questions/21415467/copy-file-into-a-specific-parent-folder-with-google-drive-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!