Folder provides a simple and efficient approach to organize files. Along with file management, Google Drive provides an option to create folders. Unlike traditional folder system, with Google Drive you can store one file under multiple folders. In the previous article, we have already seen how to work with files. This article will help you understand the basics of Folder API.
You will be amazed to see how Google has simplified the API to interact with Google Drive. The Folder is an example of that. We have already seen how to create a File using File API, it is the same API that we will use to create a folder. You just need to know the correct mime type and invoke the same file API.
Primarily a folder is a file without any content in it. But to refer it in forthcoming operations it is necessary that you should store reference of it. Yes, you guessed it right, we need folder “id” for our subsequent operations. In fact, in the next section, we will use the Folder ID to upload a file in it.
File file = new File(); file.setName("name_of_folder"); file.setMimeType("application/vnd.google-apps.folder"); drive.files().create(file).setFields("id").execute();
Upload a File in a Folder
So we created a folder on Google drive, but how do we indicate folder name while uploading file. Don’t worry, the option to do that is pretty simple. You can continue to use the same File Create API. Just make sure that you call an additional method on file object indicating the parent folder id.
Interestingly, you can specify multiple Folder IDs. Rest of the steps remain same and once you execute the API, you will find the file is uploaded inside a folder.
We have barely scratched the vast amount of Drive APIs that are available at our disposal. Feel free to comment, if you wish to see a video/article on a specific topic.