Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive folder ID

I have a folder path, for example /docs/word, and I would like to get the ID of the "word" folder (the last folder), in order to upload a file there. How do I get the ID?

like image 516
guyzyl Avatar asked Mar 07 '13 17:03

guyzyl


1 Answers

So I figured it out. What you have to do is get the id of the root drive_service.about().get().execute()["rootFolderId"] , and then get the files in root, go to the next folder in the path, etc. btw, the function i wrote to list the folders inside a path and save them to a dictionary (using 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
like image 97
guyzyl Avatar answered Sep 23 '22 05:09

guyzyl