Google Storage
Since Camel 3.9
Both producer and consumer are supported
The Google Storage component provides access to Google Cloud Storage via the Google java storage library.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-google-storage</artifactId>
<!-- use the same version as your Camel core version -->
<version>x.x.x</version>
</dependency>
Authentication Configuration
Google Storage component authentication is targeted for use with the GCP Service Accounts. For more information, please refer to Google Storage Auth Guide.
When you have the service account key, you can provide authentication credentials to your application code. Google security credentials can be set through the component endpoint:
String endpoint = "google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json";
Or by providing the path to the GCP credentials file location:
Provide authentication credentials to your application code by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS
:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"
URI Format
google-storage://bucketNameOrArn?[options]
By default, the bucket will be created if it doesn’t already exist. You can append query options to the URI in the following format: ?options=value&option2=value&…
For example, to read file hello.txt
from bucket myCamelBucket
, use the following snippet:
from("google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json&objectName=hello.txt")
.to("file:/var/downloaded");
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties
,*.yaml
files, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Google Storage component supports 21 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Setting the autocreation of the bucket bucketName. | true | boolean | |
The component configuration. | GoogleCloudStorageConfiguration | ||
The Service account key that can be used as credentials for the Storage client. It can be loaded by default from classpath, but you can prefix with classpath:, file:, or http: to load the resource from different systems. | String | ||
The Cloud Storage class to use when creating the new buckets. | STANDARD | StorageClass | |
Autowired The storage client. | Storage | ||
The Cloud Storage location to use when creating the new buckets. | US-EAST1 | String | |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
Delete objects from the bucket after they have been retrieved. The delete is only performed if the Exchange is committed. If a rollback occurs, the object is not deleted. If this option is false, then the same objects will be retrieve over and over again on the polls. | true | boolean | |
Define the destination bucket where an object must be moved when moveAfterRead is set to true. | String | ||
The folder or filename to use when downloading the blob. By default, this specifies the folder name, and the name of the file is the blob name. For example, setting this to mydownload will be the same as setting mydownload/$\{file:name}. You can use dynamic expressions for fine-grained control. For example, you can specify $\{date:now:yyyyMMdd}/$\{file:name} to store the blob in sub folders based on today’s day. Only $\{file:name} and $\{file:name.noext} is supported as dynamic tokens for the blob name. | String | ||
A regular expression to include only blobs with name matching it. | String | ||
If it is true, the Object exchange will be consumed and put into the body. If false the Object stream will be put raw into the body and the headers will be set with the object metadata. | true | boolean | |
If it is true, the folders/directories will be consumed. If it is false, they will be ignored, and Exchanges will not be created for those. | true | boolean | |
Move objects from the origin bucket to a different bucket after they have been retrieved. To accomplish the operation the destinationBucket option must be set. The copy bucket operation is only performed if the Exchange is committed. If a rollback occurs, the object is not moved. | false | boolean | |
The prefix which is used in the BlobListOptions to only consume objects we are interested in. | String | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The Object name inside the bucket. | String | ||
Set the operation for the producer. Enum values:
| GoogleCloudStorageOperations | ||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
Used for enabling or disabling all consumer based health checks from this component. | true | boolean | |
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. | true | boolean |
Endpoint Options
The Google Storage endpoint is configured using URI syntax:
google-storage:bucketName
With the following path and query parameters:
Query Parameters (35 parameters)
Name | Description | Default | Type |
---|---|---|---|
Setting the autocreation of the bucket bucketName. | true | boolean | |
The Service account key that can be used as credentials for the Storage client. It can be loaded by default from classpath, but you can prefix with classpath:, file:, or http: to load the resource from different systems. | String | ||
The Cloud Storage class to use when creating the new buckets. | STANDARD | StorageClass | |
Autowired The storage client. | Storage | ||
The Cloud Storage location to use when creating the new buckets. | US-EAST1 | String | |
Delete objects from the bucket after they have been retrieved. The delete is only performed if the Exchange is committed. If a rollback occurs, the object is not deleted. If this option is false, then the same objects will be retrieve over and over again on the polls. | true | boolean | |
Define the destination bucket where an object must be moved when moveAfterRead is set to true. | String | ||
The folder or filename to use when downloading the blob. By default, this specifies the folder name, and the name of the file is the blob name. For example, setting this to mydownload will be the same as setting mydownload/$\{file:name}. You can use dynamic expressions for fine-grained control. For example, you can specify $\{date:now:yyyyMMdd}/$\{file:name} to store the blob in sub folders based on today’s day. Only $\{file:name} and $\{file:name.noext} is supported as dynamic tokens for the blob name. | String | ||
A regular expression to include only blobs with name matching it. | String | ||
If it is true, the Object exchange will be consumed and put into the body. If false the Object stream will be put raw into the body and the headers will be set with the object metadata. | true | boolean | |
If it is true, the folders/directories will be consumed. If it is false, they will be ignored, and Exchanges will not be created for those. | true | boolean | |
Move objects from the origin bucket to a different bucket after they have been retrieved. To accomplish the operation the destinationBucket option must be set. The copy bucket operation is only performed if the Exchange is committed. If a rollback occurs, the object is not moved. | false | boolean | |
The prefix which is used in the BlobListOptions to only consume objects we are interested in. | String | ||
If the polling consumer did not poll any files, you can enable this option to send an empty message (no body) instead. | false | boolean | |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. | ExceptionHandler | ||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
| ExchangePattern | ||
A pluggable org.apache.camel.PollingConsumerPollingStrategy allowing you to provide your custom implementation to control error handling usually occurred during the poll operation before an Exchange have been created and being routed in Camel. | PollingConsumerPollStrategy | ||
The Object name inside the bucket. | String | ||
Set the operation for the producer. Enum values:
| GoogleCloudStorageOperations | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The number of subsequent error polls (failed due some error) that should happen before the backoffMultipler should kick-in. | int | ||
The number of subsequent idle polls that should happen before the backoffMultipler should kick-in. | int | ||
To let the scheduled polling consumer backoff if there has been a number of subsequent idles/errors in a row. The multiplier is then the number of polls that will be skipped before the next actual attempt is happening again. When this option is in use then backoffIdleThreshold and/or backoffErrorThreshold must also be configured. | int | ||
Milliseconds before the next poll. | 500 | long | |
If greedy is enabled, then the ScheduledPollConsumer will run immediately again, if the previous run polled 1 or more messages. | false | boolean | |
Milliseconds before the first poll starts. | 1000 | long | |
Specifies a maximum limit of number of fires. So if you set it to 1, the scheduler will only fire once. If you set it to 5, it will only fire five times. A value of zero or negative means fire forever. | 0 | long | |
The consumer logs a start/complete log line when it polls. This option allows you to configure the logging level for that. Enum values:
| TRACE | LoggingLevel | |
Allows for configuring a custom/shared thread pool to use for the consumer. By default each consumer has its own single threaded thread pool. | ScheduledExecutorService | ||
To use a cron scheduler from either camel-spring or camel-quartz component. Use value spring or quartz for built in scheduler. | none | Object | |
To configure additional properties when using a custom scheduler or any of the Quartz, Spring based scheduler. | Map | ||
Whether the scheduler should be auto started. | true | boolean | |
Time unit for initialDelay and delay options. Enum values:
| MILLISECONDS | TimeUnit | |
Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details. | true | boolean |
Message Headers
The Google Storage component supports 27 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
CamelGoogleCloudStorageOperation (producer) Constant: | The operation to perform. Enum values:
| GoogleCloudStorageOperations | |
CamelGoogleCloudStorageBucketName (producer) Constant: | The bucket Name which this object will be stored or which will be used for the current operation. | String | |
CamelGoogleCloudStorageObjectName (producer) Constant: | The object Name which will be used for the current operation. | String | |
CamelGoogleCloudStoragePrefixName (producer) Constant: | The prefix to be used in List Objects operation. | String | |
CamelGoogleCloudStorageDestinationObjectName (producer) Constant: | The object Destination Name which will be used for the current operation. | String | |
CamelGoogleCloudStorageDestinationBucketName (producer) Constant: | The bucket Destination Name which will be used for the current operation. | String | |
CamelGoogleCloudStorageDownloadLinkExpirationTime (producer) Constant: | The time in millisecond the download link will be valid. | 300000 | Long |
CamelGoogleCloudStorageContentLength (common) Constant: | The content length of this object. | Long | |
CamelGoogleCloudStorageContentType (common) Constant: | The content type of this object. | String | |
CamelGoogleCloudStorageCacheControl (common) Constant: | The Cache-Control metadata can specify two different aspects of how data is served from Cloud Storage: whether the data can be cached and whether the data can be transformed. | String | |
CamelGoogleCloudStorageContentDisposition (common) Constant: | The content disposition of this object. | String | |
CamelGoogleCloudStorageContentEncoding (common) Constant: | The content encoding of this object. | String | |
CamelGoogleCloudStorageContentMd5 (common) Constant: | The md5 checksum of this object. | String | |
Constant: | The name of the blob. | String | |
CamelGoogleCloudStorageComponentCount (consumer) Constant: | The component count of this object. | Integer | |
CamelGoogleCloudStorageContentLanguage (consumer) Constant: | The Content-Language metadata indicates the language(s) that the object is intended for. | String | |
CamelGoogleCloudStorageCustomTime (consumer) Constant: | The Custom-Time metadata is a user-specified date and time represented in the RFC 3339 format YYYY-MM-DD’T’HH:MM:SS.SS’Z' or YYYY-MM-DD’T’HH:MM:SS’Z' when milliseconds are zero. This metadata is typically set in order to use the DaysSinceCustomTime condition in Object Lifecycle Management. | Long | |
CamelGoogleCloudStorageCrc32cHex (consumer) Constant: | The CRC32c of the object. | String | |
CamelGoogleCloudStorageETag (common) Constant: | The ETag for the Object. | String | |
CamelGoogleCloudStorageGeneration (consumer) Constant: | Is the generation number of the object for which you are retrieving information. | Long | |
CamelGoogleCloudStorageBlobId (consumer) Constant: | The blob id of the object. | BlobId | |
CamelGoogleCloudStorageKmsKeyName (consumer) Constant: | The KMS key name. | String | |
CamelGoogleCloudStorageMediaLink (consumer) Constant: | The media link. | String | |
CamelGoogleCloudStorageMetageneration (consumer) Constant: | The metageneration of the object. | Long | |
CamelGoogleCloudStorageStorageClass (consumer) Constant: | The storage class of the object. | StorageClass | |
CamelGoogleCloudStorageCreateTime (consumer) Constant: | The creation time of the object. | Long | |
CamelGoogleCloudStorageLastUpdate (consumer) Constant: | The last update of the object. | Date |
Google Storage Producer operations
Google Storage component provides the following operations on the producer side:
-
copyObject
-
listObjects
-
deleteObject
-
deleteBucket
-
listBuckets
-
getObject
-
createDownloadLink
If you don’t specify an operation explicitly, the producer will a file upload.
Advanced component configuration
If you need to have more control over the storageClient
instance configuration, you can create your own instance and refer to it in your Camel google-storage component configuration:
from("google-storage://myCamelBucket?storageClient=#client")
.to("mock:result");
Google Storage Producer Operation examples
-
File Upload: This operation will upload a file to the Google Storage based on the body content
//upload a file
byte[] payload = "Camel rocks!".getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(payload);
from("direct:start")
.process( exchange -> {
exchange.getIn().setHeader(GoogleCloudStorageConstants.OBJECT_NAME, "camel.txt");
exchange.getIn().setBody(bais);
})
.to("google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json")
.log("uploaded file object:${header.CamelGoogleCloudStorageObjectName}, body:${body}");
This operation will upload the file camel.txt
with the content "Camel rocks!"
in the myCamelBucket bucket
-
CopyObject
: this operation copies an object from one bucket to a different one
from("direct:start").process( exchange -> {
exchange.getIn().setHeader(GoogleCloudStorageConstants.OPERATION, GoogleCloudStorageOperations.copyObject);
exchange.getIn().setHeader(GoogleCloudStorageConstants.OBJECT_NAME, "camel.txt" );
exchange.getIn().setHeader(GoogleCloudStorageConstants.DESTINATION_BUCKET_NAME, "myCamelBucket_dest");
exchange.getIn().setHeader(GoogleCloudStorageConstants.DESTINATION_OBJECT_NAME, "camel_copy.txt");
})
.to("google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json")
.to("mock:result");
This operation will copy the object with the name expressed in the header DESTINATION_OBJECT_NAME to the DESTINATION_BUCKET_NAME bucket, from the bucket myCamelBucket.
-
DeleteObject
: this operation deletes an object from a bucket
from("direct:start").process( exchange -> {
exchange.getIn().setHeader(GoogleCloudStorageConstants.OPERATION, GoogleCloudStorageOperations.deleteObject);
exchange.getIn().setHeader(GoogleCloudStorageConstants.OBJECT_NAME, "camel.txt" );
})
.to("google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json")
.to("mock:result");
This operation will delete the object from the bucket myCamelBucket.
-
ListBuckets
: this operation lists the buckets for this account in this region
from("direct:start")
.to("google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json&operation=listBuckets")
.to("mock:result");
This operation will list the buckets for this account.
-
DeleteBucket
: this operation deletes the bucket specified as URI parameter or header
from("direct:start")
.to("google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json&operation=deleteBucket")
.to("mock:result");
This operation will delete the bucket myCamelBucket.
-
ListObjects
: this operation list object in a specific bucket
from("direct:start")
.to("google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json&operation=listObjects")
.to("mock:result");
This operation will list the objects in the myCamelBucket bucket.
-
GetObject
: this operation gets a single object in a specific bucket
from("direct:start")
.process( exchange -> {
exchange.getIn().setHeader(GoogleCloudStorageConstants.OBJECT_NAME, "camel.txt");
})
.to("google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json&operation=getObject")
.to("mock:result");
This operation will return a Blob object instance related to the OBJECT_NAME
object in myCamelBucket
bucket.
-
CreateDownloadLink
: this operation will return a download link
from("direct:start")
.process( exchange -> {
exchange.getIn().setHeader(GoogleCloudStorageConstants.OBJECT_NAME, "camel.txt" );
exchange.getIn().setHeader(GoogleCloudStorageConstants.DOWNLOAD_LINK_EXPIRATION_TIME, 86400000L); //1 day
})
.to("google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json&operation=createDownloadLink")
.to("mock:result");
This operation will return a download link url for the file OBJECT_NAME in the bucket myCamelBucket. It’s possible to specify the expiration time for the created link through the header DOWNLOAD_LINK_EXPIRATION_TIME. If not specified, by default it is 5 minutes.
Bucket Auto creation
With the option autoCreateBucket
users are able to avoid the autocreation of a Bucket in case it doesn’t exist. The default for this option is true
. If set to false, any operation on a not-existent bucket won’t be successful and an error will be returned.
MoveAfterRead consumer option
In addition to deleteAfterRead
it has been added another option, moveAfterRead
. With this option enabled the consumed object will be moved to a target destinationBucket
instead of being only deleted. This will require specifying the destinationBucket option. As example:
from("google-storage://myCamelBucket?serviceAccountKey=/home/user/Downloads/my-key.json"
+ "&autoCreateBucket=true"
+ "&destinationBucket=myCamelProcessedBucket"
+ "&moveAfterRead=true"
+ "&deleteAfterRead=true"
+ "&includeBody=true"
)
.to("mock:result");
In this case, the objects consumed will be moved to myCamelProcessedBucket bucket and deleted from the original one (because of deleteAfterRead).
Spring Boot Auto-Configuration
When using google-storage with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-google-storage-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 22 options, which are listed below.