2013-03-07 174 views
2

我有一个文件夹路径,例如/ docs/word,我想获得“word”文件夹的ID(最后一个文件夹),以便在那里上传文件。我如何获得ID?谷歌云端硬盘文件夹ID

回答

2

所以我想通了。你需要做的是获得根drive_service.about().get().execute()["rootFolderId"]的ID,然后获取根目录中的文件,转到路径中的下一个文件夹等等。btw,我写的函数列出路径中的文件夹并将它们保存到字典(使用self.addPath())

def listFolders(self, path): 
    fId = self.getPathId(path) #get the id of the parent folder 
    files = self.drive_service.children().list(folderId=fId).execute() #Request children 
    files = files["items"] #All of the items in the folder 

    folders = [] 
    for i in range(len(files)): 
     sId = files[i]["id"] 
     sFile = self.drive_service.files().get(fileId=sId).execute() 
     if sFile["labels"]["trashed"] == False and sFile["mimeType"] == "application/vnd.google-apps.folder": 
      self.addPath(path+sFile["title"]+"/", sFile["id"]) 
      folders.append(sFile["title"]) 
    return folders