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.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
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
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.
xxxxxxxxxx
mvn quarkus:list-extensions
Add the camel extension
x
mvn quarkus:add-extension -Dextensions=org.apache.camel.quarkus:camel-quarkus-core
Quarkus camel extension being added to the pom.file
x
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core</artifactId>
</dependency>
Add New GoogleDriveRoute Class to the Project
Setting up GoogleDriveConfiguration
xxxxxxxxxx
final GoogleDriveConfiguration configuration = new GoogleDriveConfiguration();
configuration.setAccessToken(ACCESS_TOKEN);
configuration.setClientId(CLIENT_ID);
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.
xxxxxxxxxx
final CamelContext context = super.getContext();
// add GoogleDriveComponent to Camel context
final GoogleDriveComponent component = new GoogleDriveComponent(context);
component.setConfiguration(configuration);
context.addComponent("google-drive", component);
URI Format
xxxxxxxxxx
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
xxxxxxxxxx
from("timer:foo?period=1000")
.to("google-drive://drive-files/list")
.transform(simple("${body.toString()}"))
.log("${body}");
insert
xxxxxxxxxx
from("timer:foo?period=1000")
//call processor and set headers
.process(new MyProcessor())
.to("google-drive://drive-files/insert")
.log("done");
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
x
import com.google.api.client.http.FileContent;
import com.google.api.services.drive.model.File;
public class MyProcessor implements Processor {
private static final String TEST_UPLOAD_FILE = "src/main/resources/file/customerlist.csv";
private static final java.io.File UPLOAD_FILE = new java.io.File(TEST_UPLOAD_FILE);
public void process(Exchange exchange) throws Exception {
File fileMetadata = new File();
fileMetadata.setTitle(UPLOAD_FILE.getName());
FileContent mediaContent = new FileContent(null, UPLOAD_FILE);
final Map<String, Object> headers = new HashMap<>();
// parameter type is com.google.api.services.drive.model.File
headers.put("CamelGoogleDrive.content", fileMetadata);
headers.put("CamelGoogleDrive.mediaContent", mediaContent);
exchange.getIn().setHeaders(headers);
}
}
get
xxxxxxxxxx
from("timer:foo?period=10000")
.process(new MyProcessor())
.to("google-drive://drive-files/get?fileId=16t0SbrIualNnIZ27Iqy_iLTXv_CE5Zik")
.log("${body}");
'Get' method reads the file. fileId should be provided to read a specific file. fileId can be fetched using the list method
xxxxxxxxxx
[kkakarla@kkakarla camel-quarkus-gdrive-java]$ mvn clean compile quarkus:dev
[INFO] Scanning for projects...
[INFO]
[INFO] ------< org.apache.camel.quarkus:camel-quarkus-googledrive-java >-------
[INFO] Building Camel Quarkus :: Examples :: Googledrive 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ camel-quarkus-googledrive-java ---
[INFO] Deleting /home/kkakarla/development/camel-fuse-examples/Quarkus/camel-quarkus-gdrive-java/target
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (camel-quarkus-enforcer-rules) @ camel-quarkus-googledrive-java ---
[INFO]
[INFO] --- maven-remote-resources-plugin:1.5:process (process-resource-bundles) @ camel-quarkus-googledrive-java ---
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ camel-quarkus-googledrive-java ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ camel-quarkus-googledrive-java ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/kkakarla/development/camel-fuse-examples/Quarkus/camel-quarkus-gdrive-java/target/classes
[INFO]
[INFO] --- quarkus-maven-plugin:1.5.0.Final:dev (default-cli) @ camel-quarkus-googledrive-java ---
Listening for transport dt_socket at address: 5005
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!
2020-06-11 13:13:12,542 INFO [org.apa.cam.sup.LRUCacheFactory] (Quarkus Main Thread) Detected and using LRUCacheFactory: camel-caffeine-lrucache
2020-06-11 13:13:12,664 INFO [org.apa.cam.mai.BaseMainSupport] (Quarkus Main Thread) Auto-configuration summary:
2020-06-11 13:13:12,665 INFO [org.apa.cam.mai.BaseMainSupport] (Quarkus Main Thread) camel.context.name=quarkus-camel-example-timer-log
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.
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
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
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
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
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
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.
2020-06-11 13:13:12,753 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
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]
2020-06-11 13:13:15,932 INFO [route1] (Camel (quarkus-camel-example-timer-log) thread #2 - CamelGoogleDrive) name,surname
Michael,SMITH
James,JOHNSON
John,WILLIAMS
Robert,BROWN
David,JONES
William,MILLER
Mary,DAVIS
Christopher,GARCIA
delete
xxxxxxxxxx
from("timer:foo?period=10000")
.process(new MyProcessor())
.to("google-drive://drive-files/delete?fileId=16t0SbrIualNnIZ27Iqy_iLTXv_CE5Zik")
.log("${body}");
delete method deletes the file which has Id '16t0SbrIualNnIZ27Iqy_iLTXv_CE5Zik'.
xxxxxxxxxx
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
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
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
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.
2020-06-11 13:27:09,211 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
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]
2020-06-11 13:27:12,325 INFO [route1] (Camel (quarkus-camel-example-timer-log) thread #2 - CamelGoogleDrive) null
copy
xxxxxxxxxx
from("timer:foo?period=10000")
.process(new MyProcessor())
.to("google-drive://drive-files/copy?fileId=1gfZ6jCRnIwsj80KhAHpAMauoPqefOgzr")
.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
mvn compile quarkus:dev
xxxxxxxxxx
[INFO] --- quarkus-maven-plugin:1.5.0.Final:dev (default-cli) camel-quarkus-googledrive-java ---
Listening for transport dt_socket at address: 5005
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!
2020-06-11 16:57:41,442 INFO [org.apa.cam.mai.BaseMainSupport] (Quarkus Main Thread) Auto-configuration summary:
2020-06-11 16:57:41,443 INFO [org.apa.cam.mai.BaseMainSupport] (Quarkus Main Thread) camel.context.name=quarkus-camel-example-timer-log
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.
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
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
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
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
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
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.
2020-06-11 16:57:41,525 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
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]
2020-06-11 16:57:45,538 INFO [route1] (Camel (quarkus-camel-example-timer-log) thread #1 - CamelGoogleDrive) name,surname
Michael,SMITH
James,JOHNSON
John,WILLIAMS
We can see that the boot time is of 0.997s
Run the Project in native mode
xxxxxxxxxx
mvn clean package -Pnative
./target/camel-quarkus-googledrive-java-1.0-runner
xxxxxxxxxx
./target/camel-quarkus-googledrive-java-1.0-runner
2020-06-11 17:07:13,439 INFO [org.apa.cam.mai.BaseMainSupport] (main) Auto-configuration summary:
2020-06-11 17:07:13,440 INFO [org.apa.cam.mai.BaseMainSupport] (main) camel.context.name=quarkus-camel-example-timer-log
2020-06-11 17:07:13,441 WARNING [com.goo.api.cli.goo.ser.AbstractGoogleClient] (main) Application name is not set. Call Builder#setApplicationName.
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
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
2020-06-11 17:07:13,441 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (main) Route: route1 started and consuming from: timer://foo
2020-06-11 17:07:13,441 INFO [org.apa.cam.imp.eng.AbstractCamelContext] (main) Total 1 routes, of which 1 are started
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
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.
2020-06-11 17:07:13,441 INFO [io.quarkus] (main) Profile prod activated.
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]
2020-06-11 17:07:17,050 INFO [route1] (Camel (quarkus-camel-example-timer-log) thread #1 - CamelGoogleDrive) name,surname
Michael,SMITH
James,JOHNSON
John,WILLIAMS
Robert,BROWN
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.
Opinions expressed by DZone contributors are their own.
Comments