Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Google Cloud Storage support #234

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-aws2-s3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-google-storage</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-json-validator</artifactId>
Expand Down Expand Up @@ -239,6 +243,12 @@
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>gcloud</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.UUID;

@ApplicationScoped
public class S3FilesRoute extends RouteBuilder {
public class AwsS3FilesRoute extends RouteBuilder {

@ConfigProperty(name = "openubl.storage.link-expiration", defaultValue = "5000")
String linkExpiration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.ublhub.files.camel;

import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.google.storage.GoogleCloudStorageConstants;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import java.util.Iterator;
import java.util.Optional;
import java.util.UUID;

@ApplicationScoped
public class GoogleCloudStorageFilesRoute extends RouteBuilder {

@ConfigProperty(name = "openubl.storage.link-expiration", defaultValue = "5000")
String linkExpiration;

@ConfigProperty(name = "openubl.storage.gcstorage.bucket")
String gcStorageBucket;

@ConfigProperty(name = "openubl.storage.gcstorage.project_id")
String gcStorageProjectId;

@ConfigProperty(name = "openubl.storage.gcstorage.service_account_id")
String gcStorageServiceAccountId;

@ConfigProperty(name = "openubl.storage.gcstorage.host")
Optional<String> gcStorageHost;

@Named("gcStorageClient")
@Produces
public Storage gcStorageClient() {
if (gcStorageHost.isPresent()) {
StorageOptions options = StorageOptions.newBuilder()
.setHost(gcStorageHost.get())
.setProjectId(gcStorageProjectId)
// .setCredentials(NoCredentials.getInstance())
// .setRetrySettings(ServiceOptions.getNoRetrySettings())
.build();
return options.getService();
} else {
return StorageOptions.getDefaultInstance()
.getService();
}
}

@Override
public void configure() throws Exception {
from("direct:gcstorage-save-file")
.id("gcstorage-save-file")
.choice()
.when(header("shouldZipFile").isEqualTo(true))
.marshal().zipFile()
.endChoice()
.end()
.process(exchange -> {
exchange.getIn().setHeader(GoogleCloudStorageConstants.OBJECT_NAME, UUID.randomUUID().toString());
exchange.getIn().setHeader(GoogleCloudStorageConstants.BUCKET_NAME, gcStorageBucket);
})
.toD("google-storage://" + gcStorageBucket + "?autoCreateBucket=true&storageClient=#gcStorageClient")
.process(exchange -> {
String documentID = exchange.getIn().getHeader(GoogleCloudStorageConstants.OBJECT_NAME, String.class);
exchange.getIn().setBody(documentID);
});

from("direct:gcstorage-get-file")
.id("gcstorage-get-file")
.setHeader(GoogleCloudStorageConstants.OPERATION, constant("getObject"))
.setHeader(GoogleCloudStorageConstants.OBJECT_NAME, body())
.choice()
.when(header("shouldUnzip").isEqualTo(true))
.pollEnrich().simple("google-storage://" + gcStorageBucket + "?storageClient=#gcStorageClient&deleteAfterRead=false&serviceAccountKey=" + gcStorageServiceAccountId)
.setHeader(GoogleCloudStorageConstants.CONTENT_DISPOSITION, simple("$header.CamelAwsS3ContentDisposition"))
.setHeader(Exchange.CONTENT_TYPE, simple("$header.CamelAwsS3ContentType"))
.unmarshal(RouteUtils.getZipFileDataFormat())
.split(bodyAs(Iterator.class), (oldExchange, newExchange) -> newExchange)
.streaming()
.convertBodyTo(byte[].class)
.end()
.endChoice()
.otherwise()
.pollEnrich().simple("google-storage://" + gcStorageBucket + "?storageClient=#gcStorageClient&deleteAfterRead=false&serviceAccountKey=" + gcStorageServiceAccountId)
.setHeader(GoogleCloudStorageConstants.CONTENT_DISPOSITION, simple("$header.CamelAwsS3ContentDisposition"))
.setHeader(Exchange.CONTENT_TYPE, simple("$header.CamelAwsS3ContentType"))
.endChoice()
.end();

from("direct:gcstorage-get-file-link")
.id("gcstorage-get-file-link")
.setHeader(GoogleCloudStorageConstants.OPERATION, constant("createDownloadLink"))
.setHeader(GoogleCloudStorageConstants.OBJECT_NAME, body())
.setHeader(GoogleCloudStorageConstants.DOWNLOAD_LINK_EXPIRATION_TIME, constant(linkExpiration))
.toD("google-storage://" + gcStorageBucket + "?storageClient=#gcStorageClient&serviceAccountKey=" + gcStorageServiceAccountId);

from("direct:gcstorage-delete-file")
.id("gcstorage-delete-file")
.setHeader(GoogleCloudStorageConstants.OPERATION, constant("deleteObject"))
.setHeader(GoogleCloudStorageConstants.OBJECT_NAME, body())
.toD("google-storage://" + gcStorageBucket + "?storageClient=#gcStorageClient&serviceAccountKey=" + gcStorageServiceAccountId);
}

}
7 changes: 6 additions & 1 deletion application/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ openubl.storage.filesystem.folder=workspace
openubl.storage.s3.health.url=http://localhost:9000/minio/health/live
openubl.storage.s3.host=http://localhost:9000
openubl.storage.s3.region=us-east-1
openubl.storage.s3.bucket=project-openubl
openubl.storage.s3.bucket=ublhub
openubl.storage.s3.access_key_id=BQA2GEXO711FVBVXDWKM
openubl.storage.s3.secret_access_key=uvgz3LCwWM3e400cDkQIH/y1Y4xgU4iV91CwFSPC

openubl.storage.gcstorage.host=http://localhost:9000
openubl.storage.gcstorage.project_id=openubl-project
openubl.storage.gcstorage.bucket=ublhub
openubl.storage.gcstorage.service_account_id=classpath:gcstorage_serviceaccountid.json

# Basic HTTP Authentication
quarkus.http.auth.basic=true

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package io.github.project.openubl.ublhub;

import io.github.project.openubl.ublhub.containers.GoogleCloudStorageServer;
import io.github.project.openubl.ublhub.containers.MinioServer;
import io.quarkus.test.junit.QuarkusTestProfile;

Expand All @@ -27,7 +28,7 @@ public abstract class ProfileManager implements QuarkusTestProfile {
List<TestResourceEntry> testResources = new ArrayList<>();

public ProfileManager() {
testResources.add(new TestResourceEntry(MinioServer.class));
testResources.add(new TestResourceEntry(GoogleCloudStorageServer.class));
}

public abstract String getProfile();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.ublhub.containers;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.testcontainers.containers.DatastoreEmulatorContainer;
import org.testcontainers.utility.DockerImageName;

import java.util.HashMap;
import java.util.Map;

public class GoogleCloudStorageServer implements QuarkusTestResourceLifecycleManager {

private DatastoreEmulatorContainer gcStorage;

@Override
public Map<String, String> start() {
gcStorage = new DatastoreEmulatorContainer(
DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk:412.0.0-emulators")
);
gcStorage.start();

String host = gcStorage.getEmulatorEndpoint();

Map<String, String> properties = new HashMap<>();
properties.put("openubl.storage.type", "gcstorage");
properties.put("openubl.storage.gcstorage.host", "http://" + host);
return properties;
}

@Override
public void stop() {
gcStorage.stop();
}

}