The post demonstrates how to build a Google Cloud Storage client application in Java. This command line client app shows the basic logic to interact with Google Cloud Storage service and eliminates unnecessary clutter.
The application interacts with Google Cloud Storage via its JSON API using the related Google Java client library.
For more information, go to Google API Client Libraries then click on the Java link. In the menu bar click APIS, then enter Ctrl-F and search for storage, You will get this:
Click on the version link (v1, in the example). This will take you to the Cloud Storage JSON API Client Library for Java. Note that at the bottom of the page in the section “Add Library to Your Project”, there are several tabs. If you click the Maven tab, you get the dependency in JSON format to add to the pom.xml file in your project. This is an example:
<dependency> <groupId>com.google.apis</groupId> <artifactId>google-api-services-storage</artifactId> <version>v1-rev111-1.22.0</version> </dependency>
See also Putting to REST the Cloud Service APIs.
The app uses a simple UI which allows the user to perform tasks such as: list the buckets in a project, list objects in a bucket, create a bucket, create an object and so on.
You can download the code at: gcp-storage-client. See also Import a Maven Project. Please, refer to README file for the latest example code information.
For background information, see GCP Cloud Storage Background.
Application Architecture
This section describes the application components and delegates the details to the actual code implementation. The following is the app architecture:
- Main. This class is the application entry point. It performs the following tasks:
- Gets the authenticated client object authorized to access the Google Cloud Storage service.
- Reads the default settings.
- Instantiates the operations classes.
- Delegates to the SimpleUI class the display of the selection menu and the processing of the user’s input.
- User Interface
- UserInterface. Abstract class that defines the variables and methods required to implement the SimpleUI class.
- SimpleUI. It extends the UserInterface class and performs the following tasks:
- Displays a selection menu for the user.
- Processes the user’s input and calls the proper method based on the user’s selection.
- Each method calls the related Google Cloud Storage JSON API.
- Core Classes
- ProjectOperations. Contains methods to perform Google Cloud Storage project operations.
- BucketOperations. Contains methods to perform Google Cloud Storage bucket operations.
- ObjectsOperations. Contains methods to perform Google Cloud Storage object operations.
- ObjectLoaderUtility. Performs object upload. This class is just a container. The actual work is done by the contained classes:
- RandomDataBlockinputStream. Generates a random data block and repeats it to provide the stream for resumable object upload
- CustomUploadProgressListener. Implements a progress listener to be invoked during object upload.
- ObjectLoaderUtility. Performs object upload. This class is just a container. The actual work is done by the contained classes:
- Authentication.
- GoogleServiceClientAuthentication. This is an abstract class which obtains the credentials for the client application to allow the use of the requested Google service REST API.
- IGoogleServiceClientAuthentication. Defines variables and methods to authenticate clients so they can use the selected Google service REST APIs.
- AuthenticateServiceClient. Creates an authenticated client object that is authorized to access the selected Google service API.
For more information, see Create Google Service Authentication App.
- Utilities.
- IUtility. Defines fields and methods to implement the Utility class.
- Utility. Defines utility methods and variables to support the application operations such as menu creation, regions list initialization and so on.
- ServiceDefaultSettings. Reads the service client default settings from the related JSON file. The file contains information such as project ID, default e-mail and so on.
Application Workflow
The following figure shows the application time sequence (or workflow):
The first time the user starts the application, the Main class performs the following tasks:
- Reads the default settings.
- Creates authenticated storage service client.
- Initializes the operation classes.
- Initializes the SimpleUI class.
- Starts the loop to process user inputs.
The SimpleUI class loops to process the user’s commands until she terminates the loop. At that point, the application terminates.
Application Implementation
Enable Google Cloud Storage API
To build the application, you will use Eclipse. Before you can do that, assure that you have enabled the service API as described next.
- Follow the steps described in Enable Google Service API.
- Download the client credentials information in a file (for example, client_secrets.json). Follow the steps described in Create OAuth Client Credentials.
Create the Application Project
- In Eclipse, create a Maven project. For more information, see Create a Maven Project.
- Add reference to the authentication app JAR file created in Build GCP Service Client Authentication. Alternatively, and a for quickest results, import the downloaded project. For more information, see Import a Maven Project
Modify the pom.xml File
A key step in creating the application project is to configure the pom.xml file correctly to define the dependencies required to implement the client application. For more information see Define Dependencies in pom.xml.
That’s it. Happy googling with Google Cloud Storage.