构建路径 - Google Drive SDK

时间:2013-03-11 18:46:21

标签: python python-2.7 google-drive-api

我正在尝试使用Python Google Drive API为文件构建远程路径。给定一个路径,比如/folderA/FolderB/folderC/theFile.txt我想在Drive上创建folderA,然后在文件夹中创建文件夹C,然后将文件上传到folderC。

我有一个短循环:

currentParent = pathStartID
for aFolder in pathArray:
    thisFoldersID = self.createRemoteFolder(aFolder, parentID = currentParent)
    currentParent = thisFoldersID

我首先在Drive根目录中创建folderA,而folderA的ID是'pathStartID'。 pathArray包含folderB和folderC的名称。

createRemoteFolder()

看起来像这样:

def createRemoteFolder(self, folderName, parentID = None):
    # Create a folder on Drive, returns the newely created folders ID
    body = {
        'title': folderName,
        'mimeType': "application/vnd.google-apps.folder"
    }
    if parentID:
        body['parentsCollection'] = [{'id': parentID}]
        root_folder = driveFileMan.client.files().insert(body = body).execute()
    return root_folder['id']

但出于某种原因,每个文件夹都是在Google云端硬盘的根目录中创建的,而不是像我想要的那样在父文件夹中创建。

有人能发现我做错了吗?或者有更简单的方法吗?

1 个答案:

答案 0 :(得分:1)

看起来只是用于设置父级的错误属性名称。尝试:

body['parents'] = [{'id': parentID}]