1
votes

I have read/write permissions to a shared folder with another Google user.

I want to list all the files (in all the folders) in that shared folder link using drive V3 API.

What is the best way to do this? The solution currently employed is pretty slow in performance if the number of folders inside shared folders is large.

My Current solution:

1 - Finding all the folders in shared link

2 - Finding all files whose parent is folder

private void getFolderHierarchy(File Res, DriveService driveService, string localPath, string editorName, string projectName)
{
    if (Res.MimeType == "application/vnd.google-apps.folder")
    {
        m_parent += Res.Name + @"\";

        foreach (var res in ResourceFromFolder(driveService, Res.Id).ToList())
            getFolderHierarchy(res, driveService, localPath, editorName, projectName);

        m_parent = m_parent.Remove(m_parent.Length - Res.Name.Length - 1);
    }
    else if (Res.MimeType == "image/jpeg" || Res.MimeType == "image/png")
    {
        {
            if (!m_DownloadedFromEditorFileId.Contains(Res.Id))
            {           
                m_DownloadedGoogleFileId.Add(Res.Name);
                m_parent = m_parent.Remove(m_parent.Length - Res.Name.Length);
            }
        }
    }
}

public List<File> ResourceFromFolder(DriveService service, string folderId)
{
    var request = service.Files.List();
    request.PageSize = 100;
    request.Q = "'" + folderId + "'" + " in parents and trashed=false";
    request.Fields = "files(modifiedTime,id,parents,name,webContentLink,mimeType)";

    List<File> TList = new List<File>();
    do
    {
        var children = request.Execute();

        foreach (var child in children.Files)
        {
            System.Threading.Thread.Sleep(10);
            TList.Add(service.Files.Get(child.Id).Execute());
        }
        request.PageToken = children.NextPageToken;
    } while (!String.IsNullOrEmpty(request.PageToken));

    return TList;
}
1

1 Answers

0
votes

The second option would be the best way to get the files from a shared folder. As stated in this related SO post use files/list with a parent query.

GET https://www.googleapis.com/drive/v2/files?q='FOLDERID'+in+parents&key={YOUR_API_KEY}

The speed will depend of how large/many the files under the that shared folder.

Hope this helps.