VFSLib Google Drive Example

Print E-mail

Please read the following steps to use the VFSLib Java library. This example demonstrates the usage of the Google Drive © provider included in the VFSLib ZIP/TGZ archives.

The Java example code is based on the original Apache Commons VFS 2.0 classes like DefaultFileSystemManager and thus should integrate seamlessly into existing implementations.

 

 


Step 1: Download VFSLib ZIP/TGZ

First of all please download the current VFSLib ZIP/TGZ archive on your computer. Choose the archive which can be best extracted from your computer (e.g. ZIP for Windows, TGZ for UNIX, ...). The ZIP archive may be unzipped directly from Ant build scripts using the unzip task into a local directory of your choice. Of course you can extract the VFSLib ZIP archive manually with WinZIP or other ZIP extraction tools.

The listing below shows the contents of the current VFSLib archives (list shows relevant files/folders only, library versions may change for newer VFSLib versions):

    api/
    api/index.html
    example/
    example/build-examples.xml
    example/com/lf/vfslib/test/gdrive/ExampleGoogleDrive.java
    extlib/
    extlib/commons-logging-1.1.jar
    extlib/google-api-client-1.17.0-rc.jar
    extlib/google-api-services-drive-v2-rev102-1.17.0-rc.jar
    extlib/google-http-client-1.17.0-rc.jar
    extlib/google-http-client-gson-1.17.0-rc.jar
    extlib/google-http-client-jackson2-1.17.0-rc.jar
    extlib/google-http-client-jdo-1.17.0-rc.jar
    extlib/google-oauth-client-1.17.0-rc.jar
    extlib/jsr305-1.3.9.jar
    extlib/jaxen-1.1.jar
    extlib/jdom-1.0.jar
    extlib/vfs-2.0.jar
    lib/

    lib/vfslib-1.6.1.jar
    lib/vfslib-eng_US-1.6.1.jar
    lib/vfslib-ger_DE-1.6.1.jar
    license/
    README.html
    release_notes.html


The ZIP archive contains various files and resources for VFSLib. The API (Javadoc) can be displayed by opening the index.html file with your favorite Internet browser. In the example folder you can find the precompiled Google Drive example code to be executed directly or using Apache Ant.

The extlib folder contains the additional libraries needed to run the VFSLib properly. If you plan to use the Google Drive provider you will have to add the Commons libraries, Google/JSR305, and Jaxen/JDOM to the classpath. The lib folder contains the VFSLib libraries including some language packs (English and German at the moment). The English language pack is mandatory and must be included always along with the main VFSLib JAR file. The other language packs are only needed if your application supports these languages.

In the license folder you can find the VFSLib license as well as the licenses for the 3rd party JAR files residing in extlib. Please take a look at the README.html as a condensed overview of the package. The current release notes for the package can be found in the release_notes.html file.

Hint

A compatible Java Runtime Environment (JRE) must be installed prior to the installation of Apache Ant. Most developers can use their normal development environment here. Some IDEs like Netbeans which include a compatible JRE and Ant installation may support the direct execution of Ant build scripts.

 


Step 2: Running The Drive Example

The VFSLib provider for Google Drive example can be easily executed using Apache Ant. If not already installed please download and install a recent Ant for your operating system. Then you should add the Ant installation folder with the Ant executables to your local classpath so that the ant command may be executed directly from the extracted ZIP/TGZ archive folder (see secion Download VFSLib ZIP/TGZ).

Please open a command line (e.g. MS-DOS under Windows) and type the following commands:

    cd [VFSLib installation folder]\example

    ant -file build-examples.xml run-google-drive

Hint

Anyway you will need to run the example in a graphical environment since the classes display dialogs to prompt the user for the parameters necessary for Google Drive.

As figure 1 shows a dialog is being displayed then where you can specify the (evaluation) license for VFSLib. If the license is not valid the example class exits with a message. If you specified a valid license the example proceeds with showing the next dialogs.

VFSLib License Chooser

Figure 1: Google Drive example license chooser dialog

VFSLib Dropbox Application Key

Figure 2: Google Drive example client ID dialog

VFSLib Dropbox Application Secret

Figure 3: Google Drive example client secret dialog

 

You are next asked to specify ID and secret for your application. Please go create a Google Drive account on the Google website and create a project and application using the Cloud Console. Don't forget to
have the Drive API enabled for your project registered from within the Cloud Console. The ID and secret generated by Google can be used to fill in the dialog input fields (see figures 2 and 3).

If you specified an evaluation license for VFSLib now an additional dialog will show up where you have to press the "Continue" button to move on. If you have obtained a commercial license already this dialog does not appear.

VFSLib Dropbox Access Token

Figure 4: Google Drive example access token/code

 

The next dialog is displayed (see figure 4) where you can enter the code (access token) from Google. Simultaneously the example opens your favorite Internet browser showing the Google Drive website to get this code. Remember that it may be necessary to log in to Google Drive first. The access token is necessary to allow your application to access your  Google Drive account.

If all input has been done the example will continue in the background by performing some simple file operations using the specified Google Drive account. Please check out the output of the example script.

Hint

If you already have client ID and secret as well as a working access token/code from Google feel free to edit the build-examples.xml file and insert your values. You will have to uncomment the arg tag line. The input dialogs will not be displayed any longer.

 


Step 3: The Drive Example Step By Step

Let's take a closer look at the Google Drive example implementation. The first thing to do is to configure the VFSLib class which is the central class of the VFSLib library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
// Setup the main VFSLib instance, get evaluation license key from Leisenfels website
VFSLib vfslib = new VFSLib();
vfslib.setLicenseFile(licensefile); // Import from file
 
// We use the default file system manager here
DefaultFileSystemManager fsmanager = (DefaultFileSystemManager) VFS.getManager();
 
// Add Google Drive provider to VFS
GDriveFileProvider provider = vfslib.addProviderGoogleDrive(fsmanager, clientid, 
 clientsecret, appname, redirecturi);
String scheme = vfslib.getSchemeGoogleDrive();
 
// Pass access token over to VFSLib to access the Google Drive account
FileSystemOptions options = new FileSystemOptions();
GDriveFileSystemConfigBuilder builder = new GDriveFileSystemConfigBuilder();
builder.setAccessToken(options, token);
builder.setUseTrash(options, true); // Allow deleted entries be restored
 
// Setup proper account name, used as user name for Google Drive URLs (failsafe is "anonymous"):
// gdrive://[name]:[token]@drive.google.com
String username = "johndoe";
builder.setAccountDisplayName(options, username);
 

Configuration of the VFSLib (see ExampleGoogleDrive.java)

List a DriveFolder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
String uri = scheme + "://" + username + "@drive.google.com";
try {
 FileObject fileobj = fsmanager.resolveFile(uri, options);
 if (fileobj.getType().equals(FileType.FOLDER)) {
 FileObject[] childs = fileobj.getChildren();
 for (FileObject next : childs) {
 if (next.getType().equals(FileType.FOLDER)) {
 System.out.println(" DIR: " + String.valueOf(next));
 }
 else System.out.println(" FILE: " + String.valueOf(next));
 }
 }
 else System.out.println(" Entry " + uri + " is not a folder");
}
catch (Exception e) { e.printStackTrace(); }
 

List a Drive folder (see ExampleGoogleDrive.java)

Create a Drive Folder

1
2
3
4
5
6
7
8
9
10
11
12
 
String tempfolder = scheme + "://" + username + "@drive.google.com/0123456789abcdefghijklmnopqrstuvwxyz";
boolean success = false;
try {
 FileObject fileobj = fsmanager.resolveFile(tempfolder, options);
 if (!fileobj.exists()) {
 fileobj.createFolder();
 success = fileobj.exists();
 }
}
catch (Exception e) { e.printStackTrace(); }
 

Create a Drive folder (see ExampleGoogleDrive.java)

Upload a File To Drive, Get File Type, Last Modified, And Size

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
String content = "Hello world!";
String encoding = "ISO-8859-1";
 
// Upload a file to Google Drive. Get file type, last modified, and size.
String tempfile = tempfolder + "/readme.txt";
try {
 FileObject fileobj = fsmanager.resolveFile(tempfile, options);
 fileobj.getContent().setAttribute(VFSLibConstants.ATTR_CONTENT_LENGTH, content.getBytes(encoding).length);
 OutputStream ostream = fileobj.getContent().getOutputStream();
 ostream.write(content.getBytes(encoding));
 ostream.flush();
 ostream.close();
 
 success = fileobj.exists();
 
 System.out.println(" TYPE: " + fileobj.getType().getName());
 System.out.println(" MOD: " + new Date(fileobj.getContent().getLastModifiedTime()));
 System.out.println(" SIZE: " + fileobj.getContent().getSize());
}
catch (Exception e) { e.printStackTrace(); }
 

Upload a file to Drive, get file type, last modified, and size (see ExampleGoogleDrive.java)

Rename a Drive File

1
2
3
4
5
6
7
8
9
10
11
 
String tempfilerenamed = tempfolder + "/README";
try {
 FileObject fileobj = fsmanager.resolveFile(tempfile, options);
 FileObject fileobjrenamed = fsmanager.resolveFile(tempfilerenamed, options);
 fileobj.moveTo(fileobjrenamed);
 success = fileobjrenamed.exists();
 if (success) tempfile = tempfilerenamed;
}
catch (Exception e) { e.printStackTrace(); }
 

Rename a Drive file (see ExampleGoogleDrive.java)

Download a File From Drive

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
try {
 FileObject fileobj = fsmanager.resolveFile(tempfile, options);
 ByteArrayOutputStream bostream = new ByteArrayOutputStream();
 InputStream istream = fileobj.getContent().getInputStream();
 int len;
 byte[] buffer = new byte[1024];
 while ((len = istream.read(buffer)) != -1) {
 bostream.write(buffer, 0, len);
 }
 istream.close();
 bostream.flush();
 bostream.close();
 String loaded = new String(bostream.toByteArray(), encoding);
 
 success = loaded.equals(content);
}
catch (Exception e) { e.printStackTrace(); }
 

Download a file from Drive (see ExampleGoogleDrive.java)

Remove a Drive File And Folder

1
2
3
4
5
6
7
8
9
10
11
12
 
try {
 // Remove file
 FileObject fileobj = fsmanager.resolveFile(tempfile, options);
 success = fileobj.delete();
 
 // Remove folder
 fileobj = fsmanager.resolveFile(tempfolder, options);
 success = fileobj.delete();
}
catch (Exception e) { e.printStackTrace(); }
 

Remove a Drive file and folder (see ExampleGoogleDrive.java)



How to continue