gpt4 book ai didi

python - 使用 Google Drive API 将文件复制到特定的父文件夹?

转载 作者:太空狗 更新时间:2023-10-30 00:56:09 24 4
gpt4 key购买 nike

我正在使用 Python 2.7 和 service account根据标题将 Google Drive 中的文件复制到另一个文件夹中。这需要我执行五(5!)条命令:

  1. 按标题查找文件 ID。 ( files().list )
  2. 按标题查找父文件夹 ID ( files().list )
  3. 复制文件(files().copy)
  4. 将所有权转移到真实帐户 ( files().insert )
  5. 移至父文件夹。 ( parents().insert )

这一切都有效,但我想减少调用次数,首先这意味着缓存 ID,这样我就不必调用 files().list。接下来我要尝试做的事情,特别是我遇到这个问题的地方,是如何在 files().copy 命令中设置父文件夹 within .文档有一个可选的 parents 参数,描述如下:

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.

虽然它没有说,但我知道它的意思是父 ID's,因为这是在其他地方使用的。但是,在客户端库中设置此数组不会产生任何效果:没有错误并且文件肯定不在正确的文件夹中。

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

有没有人运气好with this ?还有什么我想念的吗?

奖励:在复制命令中转移所有权?也许我的服务帐户可以冒充我?

最佳答案

啊哈! parents 数组是具有 id 字段的对象列表:

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

如果/当我弄清楚如何设置权限时,我会更新这个。

这里有一个奇怪的注意事项是文件仍在被复制到驱动器根目录以及我指定的父文件夹。

关于python - 使用 Google Drive API 将文件复制到特定的父文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21415467/

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