Universal Storage - Google Drive

Universal storage provides you an interface for storing files according to your needs. With this Universal Storage Java API, you will be able to develop programs in Java and use an interface for storing your files within a google drive folder as storage.

License

License

GroupId

GroupId

org.dynamicloud.api
ArtifactId

ArtifactId

universalstorage.googledrive
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Universal Storage - Google Drive
Universal storage provides you an interface for storing files according to your needs. With this Universal Storage Java API, you will be able to develop programs in Java and use an interface for storing your files within a google drive folder as storage.
Project URL

Project URL

http://maven.apache.org
Source Code Management

Source Code Management

https://github.com/dynamicloud/universal_storage_java_gd_api.git

Download universalstorage.googledrive

How to add to project

<!-- https://jarcasting.com/artifacts/org.dynamicloud.api/universalstorage.googledrive/ -->
<dependency>
    <groupId>org.dynamicloud.api</groupId>
    <artifactId>universalstorage.googledrive</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.dynamicloud.api/universalstorage.googledrive/
implementation 'org.dynamicloud.api:universalstorage.googledrive:1.0.0'
// https://jarcasting.com/artifacts/org.dynamicloud.api/universalstorage.googledrive/
implementation ("org.dynamicloud.api:universalstorage.googledrive:1.0.0")
'org.dynamicloud.api:universalstorage.googledrive:jar:1.0.0'
<dependency org="org.dynamicloud.api" name="universalstorage.googledrive" rev="1.0.0">
  <artifact name="universalstorage.googledrive" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.dynamicloud.api', module='universalstorage.googledrive', version='1.0.0')
)
libraryDependencies += "org.dynamicloud.api" % "universalstorage.googledrive" % "1.0.0"
[org.dynamicloud.api/universalstorage.googledrive "1.0.0"]

Dependencies

compile (5)

Group / Artifact Type Version
org.dynamicloud.api : universalstorage.core jar 1.0.0
commons-io : commons-io jar 2.5
com.google.apis : google-api-services-drive jar v3-rev72-1.22.0
com.google.oauth-client : google-oauth-client-java6 jar 1.22.0
com.google.oauth-client : google-oauth-client-jetty jar 1.22.0

test (1)

Group / Artifact Type Version
junit : junit jar 3.8.1

Project Modules

There are no modules declared in this project.

Universal Storage Java API

Google Drive provider

Build Status Version

Universal storage provides you an interface for storing files according to your needs. With this Universal Storage Java API, you will be able to develop programs in Java and use an interface for storing your files within a s3 bucket as storage.


This documentation has the following content:

  1. Maven project
  2. Test API
  3. Settings
  4. Explanation for setting keys
  5. Retrieve Google Drive keys
  6. How to use

Maven project

This API follows the Maven structure to ease its installation within your project.

Test API

If you want to test the API, follow these steps:

  1. Create a folder and copy its the absolute path. This folder will be your storage root target.
  2. Create a folder and copy its the absolute path. This folder will be your tmp folder.
  3. Open with a text editor the settings.json located on test/resources/settings.json
{
	"provider": "google.drive",
	"root": "universalstorage",
	"tmp": "/home/tmp",
	"google_drive": {
		"client_id":"",
		"client_secret":"",
		"refresh_token":""
	}
}
  1. Paste the absolute paths, the root's path and the tmp's path.
  2. Save the settings.json file.

Now execute the following command:

mvn clean test

Settings

These are the steps for setting up Universal Storage in your project:

  1. You must create a file called settings.json (can be any name) and paste the following.
{
	"provider": "google.drive",
	"root": "universalstorage",
	"tmp": "/home/tmp",
	"google_drive": {
		"client_id":"",
		"client_secret":"",
		"refresh_token":""
	}
}
  1. The root and tmp keys are the main data to be filled. Create a local folder called tmp and paste its path on the key tmp.
  2. Create a folder called universalstorage in your root on Google Drive.
  3. Save the file settings.json
  4. Add the maven dependency in your pom.xml file.
<dependency>
   <groupId>org.dynamicloud.api</groupId>
   <artifactId>universalstorage.googledrive</artifactId>
   <version>1.0.0</version>
</dependency>

Explanation for setting keys

client_id key provided by Google

client_secret key provided by Google.

refresh_token This token is used to regenerate the access token.

Retrieve Google Drive keys

In order to use Google Drive as a storage, you need a Google account and retrieve three specific keys (client_id, client_secret and refresh_token). The following steps won't show how to create a google account.

  1. Create a google project here fill with the name Universal Storage.
  2. After project creation, create the credentials for this project. This step will generate two keys (client_id and client_secret). Click on Create credentials and then OAuth client ID

  1. Select as Application Type: Web application
  2. Enter a name for these new credentials
  3. Fill the field Authorized redirect URIs with: https://developers.google.com/oauthplayground
  4. Press create

  1. At this point you have generated the client_id and client_secret keys. Copy these keys and paste them into a text file, you are going to use them later.
  2. The last step is for retrieving the refresh_token. Click here to start the process.
  3. In Settings (gear icon), set
    1. Oauth flow: server
    2. Access type: offline
    3. Use your own OAuth credentials: TICK
    4. Client Id and Client Secret: the keys that you've stored within a file.
  4. Press on close

  1. On Select & authorize APIs scroll down and open the dropdown Drive API v3 then select https://www.googleapis.com/auth/drive and https://www.googleapis.com/auth/drive.file
  2. Press Authorize APIs. You will be prompted to choose your Google account and confirm access.
  3. Press Allow

  1. Press on Exchange Authorization code for tokens. Now, copy the refresh token and paste it within the file where you've stored the client_id and client_secret keys.
  2. At this point you have the three keys.
  3. Universal storage needs access to your Google Drive account. Paste these keys into the settings.json file.
{
	"provider": "google.drive",
	"root": "universalstorage",
	"tmp": "/home/tmp",
	"google_drive": {
		"client_id":"",
		"client_secret":"",
		"refresh_token":""
	}
}

This api will get the client_id, client_secret and refresh_token through either this file or environment variables client_id, client_secret and refresh_token

The root folder is the storage where the files will be stored.

The local tmp folder is where temporary files will be stored.

How to use

Examples for Storing files:

  1. Passing the settings programmatically
try {
      UniversalStorage us = UniversalStorage.Impl.
          getInstance(new UniversalSettings(new File("/home/test/resources/settings.json")));
      us.storeFile(new File("/home/test/resources/settings.json"), "myfolder/innerfolder");
      us.storeFile(new File("/home/test/resources/settings.json"));
      us.storeFile(new File("/home/test/resources/settings.json").getAbsolutePath(), "myfolder/innerfolder");
      us.storeFile(new File("/home/test/resources/settings.json").getAbsolutePath());
} catch (UniversalStorageException e) {
    fail(e.getMessage());
}
  1. The settings could be passed through either jvm parameter or environment variable.
  2. If you want to pass the settings.json path through jvm parameter, in your java command add the following parameter: -Duniversal.storage.settings=/home/test/resources/settings.json
  3. If your want to pass the settings.json path through environment variable, add the following variable: universal_storage_settings=/home/test/resources/settings.json
try {
      UniversalStorage us = UniversalStorage.Impl.getInstance();
      us.storeFile(new File("/home/test/resources/settings.json"), "myfolder/innerfolder");
      us.storeFile(new File("/home/test/resources/settings.json"));
      us.storeFile(new File("/home/test/resources/settings.json").getAbsolutePath(), "myfolder/innerfolder");
      us.storeFile(new File("/home/test/resources/settings.json").getAbsolutePath());
} catch (UniversalStorageException e) {
    fail(e.getMessage());
}

Remove file:

try {
      UniversalStorage us = UniversalStorage.Impl.getInstance();
      us.removeFile("/home/test/resources/settings.json");
} catch (UniversalStorageException e) {
    e.printStackTrace();
}

Create folder:

try {
      UniversalStorage us = UniversalStorage.Impl.getInstance();
      us.createFolder("/myNewFolder");
} catch (UniversalStorageException e) {
    e.printStackTrace();
}

Remove folder:

try {
      UniversalStorage us = UniversalStorage.Impl.getInstance();
      us.removeFolder("/myNewFolder");
} catch (UniversalStorageException e) {
    e.printStackTrace();
}

Retrieve file:

try {
      UniversalStorage us = UniversalStorage.Impl.getInstance();
      us.retrieveFile("myFolder/file.txt");
} catch (UniversalStorageException e) {
    e.printStackTrace();
}

Retrieve file as InputStream:

This inputstream will use a file that was stored into the tmp folder.

try {
      UniversalStorage us = UniversalStorage.Impl.getInstance();
      InputSstream stream = us.retrieveFileAsStream("myFolder/file.txt");
} catch (UniversalStorageException e) {
    e.printStackTrace();
}

Clean up tmp folder:

try {
      UniversalStorage us = UniversalStorage.Impl.getInstance();
      us.clean();
} catch (UniversalStorageException e) {
    e.printStackTrace();
}

Wipe root folder:

try {
      UniversalStorage us = UniversalStorage.Impl.getInstance();
      us.wipe();
} catch (UniversalStorageException e) {
    e.printStackTrace();
}

Register listeners

This API provides useful listeners for asynchronous situations.

Your custom listener must implement this interface. This interface provides a series of methods for every situation, for example, a listener when the method "storeFile" is either starting or ending, when error occurs during any kind of process, Etc.

public interface UniversalStorageListener {}

Register a listener

UniversalStorage us = UniversalStorage.Impl.getInstance();
us.registerListener(new UniversalStorageListenerAdapter() {
     public void onFolderCreated(UniversalStorageData data) {
         System.out.println(data.toString());
     }

     public void onFileStored(UniversalStorageData data) {
         System.out.println(data.toString());
     }

     public void onError(UniversalIOException error) {
         System.out.println(error.getMessage());
     }
 });

Listener adapter

This adapter is useful for situation where you're needing only one or two implementations of UniversalStorageListener interface.

public class UniversalStorageListenerAdapter implements UniversalStorageListener {
    /**
     * This method will be called just before storing process.
     */
    public void onStoreFile() {

    }

    /**
     * This method will be called just before creation process.
     */
    public void onCreateFolder() {
        
    }

    /**
     * This method will be called just before file removing process.
     */
    public void onRemoveFile() {
        
    }

    /**
     * This method will be called just before folder removing process.
     */
    public void onRemoveFolder() {
        
    }

    /**
     * This method will be called when an error occurs.
     */
    public void onError(UniversalIOException error) {
        
    }

    /**
     * This method will be called just after storing process.
     * 
     * @param data contains data about the new file.
     */
    public void onFileStored(UniversalStorageData data) {
        
    }

    /**
     * This method will be called just after creation process.
     * 
     * @param data contains data about the new folder.
     */
    public void onFolderCreated(UniversalStorageData data) {
        
    }

    /**
     * This method will be called just after file removing process.
     */
    public void onFileRemoved() {
        
    }

    /**
     * This method will be called just after folder removing process.
     */
    public void onFolderRemoved() {
        
    }
}

Versions

Version
1.0.0