I’m trying to export all my Google Drive files and folders into a structured format like JSON or XML. The issue I’m facing is with performance when fetching file details.
Currently I’m using this approach to get file names:
DriveService driveAPI = new DriveService(credentials);
String title = driveAPI.files().get(itemId).execute().getName();
And to retrieve file IDs I do:
FileList.Request query = driveAPI.files().list();
FileList results = query.execute();
String itemId = results.getFiles().get(0).getId();
The main problem is this line:
String title = driveAPI.files().get(itemId).execute().getName();
It’s extremely slow when processing each individual file. Since I want to build a complete backup file containing all my Drive data, this method would take hours to complete.
I need the file names to properly represent my Drive’s folder structure in the export. Has anyone encountered this performance issue before and found a better solution?
you’re hammering the API with too many calls. skip the individual get() requests and use the fields parameter in your list call instead. try setFields("files(id,name,parents)") to grab all the names in one shot. cut my backup time way down when i did this last month.
You’re hitting that bottleneck because you’re making individual API calls for each file. I ran into this exact same problem building a Drive backup tool last year. Here’s the fix: grab all the metadata you need in one batch using the fields parameter in your list request. Don’t call get() for each file separately - modify your list query like this:
FileList.Request query = driveAPI.files().list().setFields("files(id,name,parents,mimeType,size,modifiedTime)");
This pulls all the file info including names in one shot. The performance difference is huge - I went from 2+ hours for ~5000 files down to under 10 minutes. If you’ve got tons of files, throw in setPageSize(1000) to cut down API calls even more. The Drive API’s built to return all that metadata efficiently through the list endpoint, so you don’t need those separate get calls.
I’ve dealt with this exact problem during a Drive migration project. The main issue is all those individual get() calls - they’ll kill your performance. Use batch requests instead. You can bundle multiple get() calls into a single HTTP request, and the Drive API handles it natively. Also, tweak your list() function to only grab the metadata fields you actually need for the backup. This made a huge difference for me - went from several hours down to about 30 minutes, even with tons of files.