使用Google Drive API将文件复制到特定的父文件夹中? [英] Copy file into a specific parent folder with Google Drive API?

查看:138
本文介绍了使用Google Drive API将文件复制到特定的父文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python 2.7和服务帐户根据其标题将Google云端硬盘中的文件复制到其他文件夹中。这需要我执行五个(5!)命令:

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. 按标题查找文件ID。 ( files()。list

  2. 按标题查找父文件夹ID( files()。list

  3. 复制文件( files()。copy ) li>
  4. 将所有权转让给真实帐户( files()。insert

  5. 移到父文件夹中。 ( parents()。insert

  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)

这一切都有效,但我想减少呼叫数量,首先这意味着缓存ID所以我不必调用 files()。list 。我试图做的下一件事,特别是我在处理这个问题时,是如何在 files()中设置父文件夹。copy code>命令。该文档具有如下所述的可选的父母参数:

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.

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

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?

父母数组是包含 id 字段的对象列表:

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.

这里有一个奇怪的提示是文件仍然被复制到驱动器根目录以及父文件夹(s)我指定。

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.

这篇关于使用Google Drive API将文件复制到特定的父文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆