Finding directories with image files using Google Drive API efficiently

I’m working on a project where I need to find all directories that have image files in them using the Google Drive API. Right now my approach is pretty slow - I fetch every single file that has “image/” in the mimeType field, then I collect all the parent folder IDs from those results and remove duplicates. This method works but it takes forever when dealing with large amounts of data.

I’m wondering if there’s a better way to do this with a specific search query parameter that could handle this logic server-side instead. I’m using vanilla JavaScript with XMLHttpRequest to make the API calls. Has anyone found a more efficient solution for this kind of folder filtering based on content type?

your approach looks good. try the search operator parents in 'folder_id' if you’re targeting specific directories. also, ditch XMLHttpRequest for fetch() with Promise.all() - lets you run parallel requests and made a huge difference in my project. the api limits suck, but client-side optimization really helps.

Google Drive API doesn’t support server-side filtering for folders with specific file types - frustrating, I know. But you can speed things up quite a bit. Use the fields parameter to grab only what you need: fields='files(id,parents)' instead of pulling full file objects. This cuts down payload size and transfer time significantly. Set pageSize to 1000 to reduce API calls, and batch multiple requests using the batch endpoint when you need follow-up folder details. The performance boost from less data transfer usually beats any client-side processing overhead, especially if you cache the parent folder mapping between sessions.

You’re doing it right - the Drive API doesn’t have native folder filtering, so this is the standard approach. However, you can significantly improve your performance. Implement pagination with error handling and rate limiting. I recommend running a maximum of 3-5 concurrent requests to avoid reaching your quota limits, as going beyond that will result in throttling. Additionally, leverage the q parameter effectively; if you know certain parent directories, combine mimeType filtering with folder restrictions. Caching results locally is key, and using modifiedTime to only refresh what has changed can also help. Remember, the real bottleneck here often lies in network latency, so minimizing round trips through smart caching can make a noticeable difference.