How to directly upload files to designated folder using Google Drive API V2 in Java

I’m working with the Google Drive V2 API in Java and need help with file uploads. Currently I’m using this approach to put files in a specific folder:

  1. First upload the file to root directory using Drive.Files.insert(File,AbstractInputStream)
  2. Remove the root parent reference from the newly uploaded file
  3. Set the target folder as the parent reference for the file

This method works but has a problem. When the internet connection is slow, users can see the file appearing in the root directory before it gets moved to the correct folder. This looks unprofessional and confusing.

Is there a way to combine these three steps into one batch operation? I know that Google Drive API supports batching for similar operation types (like multiple Files operations together or multiple Parent operations together). But can I batch different types of operations like Files.insert() with Parent.delete() in a single request?

Any suggestions on how to make this file upload process smoother would be really helpful. Thanks for your time!

you don’t need all that. just set the parent folder id in the file object before calling insert(). create your file, use setParents() with the target folder id, then insert - it uploads straight to the right folder without hitting root first.

The batching approach won’t work here. Google Drive API batching only supports operations of the same type - you can’t mix Files.insert() with Parent operations in one batch request.

Bob_Clever’s right though. Skip your three-step process and just specify the parent folder during upload. When you create your File object, use setParents() with your target folder ID before running the insert. The file uploads directly to the destination folder and never shows up in root.

I’ve done this in production apps and it fixes the temporary visibility issue completely. The upload goes straight to the right location no matter how slow your connection is.

Had this exact problem two years ago with a document management system. The flickering drove our clients nuts during big file uploads. Here’s what worked: set the parent before inserting, but watch out for the parent ID handling. In V2 API, you can’t just pass the ID string directly - you need a ParentReference object with the folder ID. Create your File object, make a new ParentReference with your target folder ID, add it to the file’s parents collection, then insert. This kills the root directory flicker completely since the API knows where the file goes right from upload start.