File operation in Google Drive API with Spring Boot

Spread the love
  • 1
    Share

A file is an efficient option to organize related data. With the nature of today’s industry, it becomes necessary to make these files available anytime, anywhere. With Google drive, one can easily share files with others. Unlike past, wherein a separate hardware like an external drive, USB pen drive, Compact Disk etc., was required to transport data. On contrary, web-hosted storage solutions like Google Drive provide lot more convenience. Because File storage is its fundamental feature, it also provides options to secure it from an unauthorized access.

Picture depicts person handling files and envelope.

Photo by Dương Trần Quốc on Unsplash

We discovered an approach to integrate Google Drive dependencies in Spring Boot project. This sets up ground to explore and get familiar with 3 basic features of File operations

  1. Create File
  2. List File(s)
  3. Deleting a File

Create File

To perform any operation on Google Drive, it is necessary to pass required credentials. Fortunately, there is no need to exchange email address and password for every API call. As we have seen in the previous videos, Google Drive API comes with useful built-in objects.

com.google.api.client.auth.oauth2.Credential – Interact with API using Access token. It also refreshes the expired token using a refresh token.

com.google.api.service.drive.Drive – Abstracts REST API calls in user-friendly Java objects. The drive object can be constructed using the builder available inside Drive class itself. 

File Metadata

Once the Credential and Drive object is constructed, our job will become easier. Creating a file requires passing few information to the Drive. This information indicates the type of file, name and content that need to be stored against the file. 

com.google.api.service.drive.model.File – Instantiate this object to pass file metadata along with API call. Here is the snippet of creating a File object.

File file = new File();
file.setName("sample.jpg");

FileContent content = new FileContent("image/jpeg", new java.io.File("/absolute/path/to/file_with_extension"));

File uploadedFile = drive.files().create(file, content).setFields("id").execute();

Note the create method has been invoked with a reference of file and content object. Each file stored on Google Drive gets a unique identifier. This identifier is crucial to execute operations on a file.

List File

If file creation was so easy with the API, listing a file is even much simpler. Once again, a reference of Credential and Drive is required before proceeding with list files API.

The list() method in Drive object will help us to fetch a list of file from Google Drive. Well, there is one catch! The Drive API requires a list of attributes to be fetched for the file (Check Google Drive REST API documentation for valid attribute list). It doesn’t return any attribute by default. The simple syntax to fetch files using attributes looks like below

List<FileList> fileList = drive.files().list().setFields("files(id,name)").execute();
for(File file : fileList.getFiles()) {
// Accessor methods are available in file object.
}

Delete File

As noted earlier, the File Identifier is required to perform any operation on file instance. If you wish to delete a file on a Drive using API, you must know it’s identifier in advance. There are 3 ways to get file identifier

  1. Generate identifier well in advance and use it later while creating files.
  2. Request file identifier while executing create operation on Drive object.
  3. Use list file API and extract identifier for a file.

Once the file Identifier is available, delete operation can be executed in a single line.

drive.files().delete("file_id").execute();

CAUTION: The delete API call removes the file permanently. There is no way to retrieve the deleted file later on.

Wasn’t it pretty easy to interact with Files on Google Drive using it’s REST API? In next post, we will explore how to create folder and perform operations on it. If you have missed any of my previous videos, you can check entire list below.

Leave a Comment.