{{announcement.body}}
{{announcement.title}}

How to Read/Write Files From Google Drive Using Camel-Quarkus

DZone 's Guide to

How to Read/Write Files From Google Drive Using Camel-Quarkus

This article shows how to Read/Write files from Google drive using camel-Quarkus-google-drive component.

· Integration Zone ·
Free Resource

Introduction

The Google Drive component provides access to the Google Drive file storage service via the Google Drive Web APIs.

Google Drive uses the OAuth 2.0 protocol for authenticating a Google account and authorizing access to user data. Before you can use this component, you will need to create an account and generate OAuth credentials. Credentials comprise of a clientId, clientSecret, and a refreshToken. A handy resource for generating a long-lived refreshToken is the OAuth playground.

Camel Quarkus

Quarkus is a Kubernetes Native Java framework tailored for GraalVM and HotSpot, crafted from best-of-breed Java libraries and standards. The goal of Quarkus is to make Java a leading platform in Kubernetes and serverless environments while offering developers a unified reactive and imperative programming model to optimally address a wider range of distributed application architectures.

Quarkus is a Java platform offering fast boot times and a low memory footprint. It targets both stock JVMs and GraalVM

Quarkus also includes an extension framework that third-party framework authors can leverage to extend it. The Quarkus extension framework reduces the complexity of making third-party frameworks run on Quarkus and compile to a GraalVM native binary.

Apache Camel is an open-source integration framework based on known Enterprise Integration Patterns. Camel empowers you to define routing and mediation rules in a variety of domain-specific languages like Java, Scala, Spring

This Tutorial Explains how easily you can read/write files  from Google drive  using camel-quarkus-google-drive component

Prerequisites

1. Generate OAuth Credentials

2. Enable google-drive API and set client credentials

3. Install GraalVM

Create a Simple Project Using the Quarkus-Maven-Plugin

Refer  quarkus-get-started.docx for detailed steps on how to create a project using quarkus-maven-plugin 

Add Camel to the Quarkus

Quarkus already offers a variety of extensions and Camel is one of those

List the available extensions.

Shell
 




xxxxxxxxxx
1


1
mvn quarkus:list-extensions



Add the camel extension

Shell
 




x


 
1
mvn quarkus:add-extension -Dextensions=org.apache.camel.quarkus:camel-quarkus-core



Quarkus camel extension being added to the pom.file

Shell
 




x


1
 <dependency>
2
      <groupId>org.apache.camel.quarkus</groupId>
3
      <artifactId>camel-quarkus-core</artifactId>
4
    </dependency>



Add New GoogleDriveRoute Class to the Project

Setting up GoogleDriveConfiguration

Java
 




xxxxxxxxxx
1


1
 final GoogleDriveConfiguration configuration = new GoogleDriveConfiguration();
2
        configuration.setAccessToken(ACCESS_TOKEN);
3
        configuration.setClientId(CLIENT_ID);
4
        configuration.setClientSecret(CLIENT_SECRET);



ACCESS_TOKEN generated from Generate OAuth Credentials and CLIENT_ID, CLIENT_SECRET generated from Enable google-drive API and set client credentials.

Add Google Drive Component to Camel Context.

Java
 




xxxxxxxxxx
1


1
final CamelContext context = super.getContext();
2
        
3
// add GoogleDriveComponent  to Camel context
4
   final GoogleDriveComponent component = new GoogleDriveComponent(context);
5
   component.setConfiguration(configuration);       
6
   context.addComponent("google-drive", component);



URI Format

Shell
 




xxxxxxxxxx
1


 
1
google-drive://endpoint-prefix/endpoint?[options]



Endpoint prefix can be one of:

  • drive-about

  • drive-apps

  • drive-changes

  • drive-channels

  • drive-children

  • drive-comments

  • drive-files

  • drive-parents

  • drive-permissions

  • drive-properties

  • drive-realtime

  • drive-replies

  • drive-revisions

For complete info regarding  path parameters, query parameters, producers and consumer options refer  camel GoogleDriveComponent page

Camel Routes

list

Java
 




xxxxxxxxxx
1


1
 from("timer:foo?period=1000")
2
                .to("google-drive://drive-files/list")
3
                .transform(simple("${body.toString()}"))
4
                .log("${body}");


 

insert

Java
 




xxxxxxxxxx
1


1
from("timer:foo?period=1000")
2
        //call processor and set headers
3
        .process(new MyProcessor())
4
        .to("google-drive://drive-files/insert")
5
        .log("done");
6
 
          
7
 
          
8
 
          



My Processor has headers CamelGoogleDrive.content set for com.google.api.services.drive.model.File and CamelGoogleDrive.mediaContent set for com.google.api.client.http.FileContent for insert method to add file to google drive

Java
 




x


 
1
import com.google.api.client.http.FileContent;
2
import com.google.api.services.drive.model.File;
3
 
          
4
public class MyProcessor implements Processor {
5
    
6
    private static final String TEST_UPLOAD_FILE = "src/main/resources/file/customerlist.csv";
7
    private static final java.io.File UPLOAD_FILE = new java.io.File(TEST_UPLOAD_FILE);
8
 
          
9
    @Override
10
    public void process(Exchange exchange) throws Exception {
11
       
12
        File fileMetadata = new File();
13
        fileMetadata.setTitle(UPLOAD_FILE.getName());
14
        FileContent mediaContent = new FileContent(null, UPLOAD_FILE);
15
        
16
        final Map<String, Object> headers = new HashMap<>();
17
        // parameter type is com.google.api.services.drive.model.File
18
        headers.put("CamelGoogleDrive.content", fileMetadata);
19
        headers.put("CamelGoogleDrive.mediaContent", mediaContent); 
20
        exchange.getIn().setHeaders(headers);
21
       
22
    }
23
 
          
24
}



get

Java
 




xxxxxxxxxx
1


1
from("timer:foo?period=10000")
2
      .process(new MyProcessor())
3
      .to("google-drive://drive-files/get?fileId=16t0SbrIualNnIZ27Iqy_iLTXv_CE5Zik")
4
        .log("${body}");



'Get' method reads the file. fileId should be provided to read a specific file. fileId can be fetched using the list method 

Shell
 




xxxxxxxxxx
1
48


 
1
[kkakarla@kkakarla camel-quarkus-gdrive-java]$ mvn clean compile quarkus:dev
2
[INFO] Scanning for projects...
3
[INFO] 
4
[INFO] ------< org.apache.camel.quarkus:camel-quarkus-googledrive-java >-------
5
[INFO] Building Camel Quarkus :: Examples :: Googledrive 1.0
6
[INFO] --------------------------------[ jar ]---------------------------------
7
[INFO] 
8
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ camel-quarkus-googledrive-java ---
9
[INFO] Deleting /home/kkakarla/development/camel-fuse-examples/Quarkus/camel-quarkus-gdrive-java/target
10
[INFO] 
11
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (camel-quarkus-enforcer-rules) @ camel-quarkus-googledrive-java ---
12
[INFO] 
13
[INFO] --- maven-remote-resources-plugin:1.5:process (process-resource-bundles) @ camel-quarkus-googledrive-java ---
14
[INFO] 
15
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ camel-quarkus-googledrive-java ---
16
[INFO] Using 'UTF-8' encoding to copy filtered resources.
17
[INFO] Copying 2 resources
18
[INFO] Copying 3 resources
19
[INFO] 
20
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ camel-quarkus-googledrive-java ---
21
[INFO] Changes detected - recompiling the module!
22
[INFO] Compiling 2 source files to /home/kkakarla/development/camel-fuse-examples/Quarkus/camel-quarkus-gdrive-java/target/classes
23
[INFO] 
24
[INFO] --- quarkus-maven-plugin:1.5.0.Final:dev (default-cli) @ camel-quarkus-googledrive-java ---
25
Listening for transport dt_socket at address: 5005
26
2020-06-11 13:13:11,688 WARN  [io.qua.dep.QuarkusAugmentor] (main) Using Java versions older than 11 to build Quarkus applications is deprecated and will be disallowed in a future release!
27
2020-06-11 13:13:12,542 INFO  [org.apa.cam.sup.LRUCacheFactory] (Quarkus Main Thread) Detected and using LRUCacheFactory: camel-caffeine-lrucache
28
2020-06-11 13:13:12,664 INFO  [org.apa.cam.mai.BaseMainSupport] (Quarkus Main Thread) Auto-configuration summary:
29
2020-06-11 13:13:12,665 INFO  [org.apa.cam.mai.BaseMainSupport] (Quarkus Main Thread)   camel.context.name=quarkus-camel-example-timer-log
30
2020-06-11 13:13:12,740 WARNING [com.goo.api.cli.goo.ser.AbstractGoogleClient] (Quarkus Main Thread) Application name is not set. Call Builder#setApplicationName.
31
2020-06-11 13:13:12,746 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Apache Camel 3.3.0 (CamelContext: quarkus-camel-example-timer-log) is starting
32
2020-06-11 13:13:12,746 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
33
2020-06-11 13:13:12,749 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Route: route1 started and consuming from: timer://foo
34
2020-06-11 13:13:12,751 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Total 1 routes, of which 1 are started
35
2020-06-11 13:13:12,751 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Apache Camel 3.3.0 (CamelContext: quarkus-camel-example-timer-log) started in 0.005 seconds
36
2020-06-11 13:13:12,752 INFO  [io.quarkus] (Quarkus Main Thread) camel-quarkus-googledrive-java 1.0 on JVM (powered by Quarkus 1.5.0.Final) started in 1.103s. 
37
2020-06-11 13:13:12,753 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
38
2020-06-11 13:13:12,753 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [camel-core, camel-file, camel-google-drive, camel-log, camel-support-common, camel-support-google-http-client, camel-support-xml, camel-timer, camel-xml-jaxb, cdi]
39
2020-06-11 13:13:15,932 INFO  [route1] (Camel (quarkus-camel-example-timer-log) thread #2 - CamelGoogleDrive) name,surname
40
Michael,SMITH
41
James,JOHNSON
42
John,WILLIAMS
43
Robert,BROWN
44
David,JONES
45
William,MILLER
46
Mary,DAVIS
47
Christopher,GARCIA
48
 
          



delete

Java
 




xxxxxxxxxx
1


 
1
from("timer:foo?period=10000")
2
        .process(new MyProcessor())
3
        .to("google-drive://drive-files/delete?fileId=16t0SbrIualNnIZ27Iqy_iLTXv_CE5Zik")
4
        .log("${body}");



delete method deletes the file which has Id '16t0SbrIualNnIZ27Iqy_iLTXv_CE5Zik'. 

Java
 




xxxxxxxxxx
1


 
1
2020-06-11 13:27:09,207 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Route: route1 started and consuming from: timer://foo
2
2020-06-11 13:27:09,209 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Total 1 routes, of which 1 are started
3
2020-06-11 13:27:09,209 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Apache Camel 3.3.0 (CamelContext: quarkus-camel-example-timer-log) started in 0.004 seconds
4
2020-06-11 13:27:09,210 INFO  [io.quarkus] (Quarkus Main Thread) camel-quarkus-googledrive-java 1.0 on JVM (powered by Quarkus 1.5.0.Final) started in 1.001s. 
5
2020-06-11 13:27:09,211 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
6
2020-06-11 13:27:09,211 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [camel-core, camel-file, camel-google-drive, camel-log, camel-support-common, camel-support-google-http-client, camel-support-xml, camel-timer, camel-xml-jaxb, cdi]
7
2020-06-11 13:27:12,325 INFO  [route1] (Camel (quarkus-camel-example-timer-log) thread #2 - CamelGoogleDrive) null
8
 
          



copy

Java
 




xxxxxxxxxx
1


1
 from("timer:foo?period=10000")
2
        .process(new MyProcessor())
3
        .to("google-drive://drive-files/copy?fileId=1gfZ6jCRnIwsj80KhAHpAMauoPqefOgzr")
4
        .to("file:src/main/resources/drive");



copy  method Downloads the file  to the file: src/main/resources/drive location

Run the Project in Dev mode

Java


Java
 




xxxxxxxxxx
1
19


1
[INFO] --- quarkus-maven-plugin:1.5.0.Final:dev (default-cli) @ camel-quarkus-googledrive-java ---
2
Listening for transport dt_socket at address: 5005
3
2020-06-11 16:57:40,568 WARN  [io.qua.dep.QuarkusAugmentor] (main) Using Java versions older than 11 to build Quarkus applications is deprecated and will be disallowed in a future release!
4
2020-06-11 16:57:41,442 INFO  [org.apa.cam.mai.BaseMainSupport] (Quarkus Main Thread) Auto-configuration summary:
5
2020-06-11 16:57:41,443 INFO  [org.apa.cam.mai.BaseMainSupport] (Quarkus Main Thread)   camel.context.name=quarkus-camel-example-timer-log
6
2020-06-11 16:57:41,513 WARNING [com.goo.api.cli.goo.ser.AbstractGoogleClient] (Quarkus Main Thread) Application name is not set. Call Builder#setApplicationName.
7
2020-06-11 16:57:41,519 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Apache Camel 3.3.0 (CamelContext: quarkus-camel-example-timer-log) is starting
8
2020-06-11 16:57:41,520 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
9
2020-06-11 16:57:41,522 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Route: route1 started and consuming from: timer://foo
10
2020-06-11 16:57:41,523 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Total 1 routes, of which 1 are started
11
2020-06-11 16:57:41,523 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (Quarkus Main Thread) Apache Camel 3.3.0 (CamelContext: quarkus-camel-example-timer-log) started in 0.004 seconds
12
2020-06-11 16:57:41,524 INFO  [io.quarkus] (Quarkus Main Thread) camel-quarkus-googledrive-java 1.0 on JVM (powered by Quarkus 1.5.0.Final) started in 0.997s. 
13
2020-06-11 16:57:41,525 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
14
2020-06-11 16:57:41,525 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [camel-core, camel-file, camel-google-drive, camel-log, camel-support-common, camel-support-google-http-client, camel-support-xml, camel-timer, camel-xml-jaxb, cdi]
15
2020-06-11 16:57:45,538 INFO  [route1] (Camel (quarkus-camel-example-timer-log) thread #1 - CamelGoogleDrive) name,surname
16
Michael,SMITH
17
James,JOHNSON
18
John,WILLIAMS
19
 
          



We can see that the boot time is of  0.997s

Run the Project in native mode

Java
 




xxxxxxxxxx
1


1
 mvn clean package -Pnative
2
  ./target/camel-quarkus-googledrive-java-1.0-runner 



Java
 




xxxxxxxxxx
1
18


1
./target/camel-quarkus-googledrive-java-1.0-runner 
2
2020-06-11 17:07:13,439 INFO  [org.apa.cam.mai.BaseMainSupport] (main) Auto-configuration summary:
3
2020-06-11 17:07:13,440 INFO  [org.apa.cam.mai.BaseMainSupport] (main)  camel.context.name=quarkus-camel-example-timer-log
4
2020-06-11 17:07:13,441 WARNING [com.goo.api.cli.goo.ser.AbstractGoogleClient] (main) Application name is not set. Call Builder#setApplicationName.
5
2020-06-11 17:07:13,441 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) Apache Camel 3.3.0 (CamelContext: quarkus-camel-example-timer-log) is starting
6
2020-06-11 17:07:13,441 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
7
2020-06-11 17:07:13,441 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) Route: route1 started and consuming from: timer://foo
8
2020-06-11 17:07:13,441 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) Total 1 routes, of which 1 are started
9
2020-06-11 17:07:13,441 INFO  [org.apa.cam.imp.eng.AbstractCamelContext] (main) Apache Camel 3.3.0 (CamelContext: quarkus-camel-example-timer-log) started in 0.000 seconds
10
2020-06-11 17:07:13,441 INFO  [io.quarkus] (main) camel-quarkus-googledrive-java 1.0 native (powered by Quarkus 1.5.0.Final) started in 0.007s. 
11
2020-06-11 17:07:13,441 INFO  [io.quarkus] (main) Profile prod activated. 
12
2020-06-11 17:07:13,441 INFO  [io.quarkus] (main) Installed features: [camel-core, camel-file, camel-google-drive, camel-log, camel-support-common, camel-support-google-http-client, camel-support-xml, camel-timer, camel-xml-jaxb, cdi]
13
2020-06-11 17:07:17,050 INFO  [route1] (Camel (quarkus-camel-example-timer-log) thread #1 - CamelGoogleDrive) name,surname
14
Michael,SMITH
15
James,JOHNSON
16
John,WILLIAMS
17
Robert,BROWN
18
 
          



We can see that the boot time now in native mode is of  0.007s

The complete project is available in git hub

Conclusion

Quarkus is so amazing in reducing boot times and results in tremendous performance improvement.

Topics:
apache, camel, google drive, graalvm, quarkus

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}