問題描述
我使用的是 Python 2.7 和 服務帳號 根據標題將 Google Drive 中的文件復制到另一個文件夾中.這需要我執行五 (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:
- 按標題查找文件 ID.(
files().list
) - 按標題查找父文件夾 ID (
files().list
) - 復制文件(
files().copy
) - 將所有權轉讓給真實帳戶(
files().insert
) - 移入父文件夾.(
parents().insert
)
這一切都有效,但我想減少調用次數,首先這意味著緩存 ID,這樣我就不必調用 files().list
.我要做的下一件事,特別是我遇到這個問題的地方,是如何在 files().copy
命令中設置父文件夾 within.該文檔有一個可選的 parents
參數,描述如下:
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,因為這是在其他地方使用的.但是,在客戶端庫中設置這個數組并不會產生效果:沒有錯誤并且文件肯定不在正確的文件夾中.
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?
推薦答案
啊哈!parents
數組是具有 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.
這里有個奇怪的提示是文件仍然被復制到驅動器根目錄以及我指定的父文件夾.
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 將文件復制到特定的父文件夾中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!