All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.kestra.storage.gcs.GcsClientFactory Maven / Gradle / Ivy

There is a newer version: 0.19.0
Show newest version
package io.kestra.storage.gcs;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import io.micronaut.context.annotation.Factory;
import lombok.SneakyThrows;

import java.io.ByteArrayInputStream;
import jakarta.inject.Singleton;

@Factory
@GcsStorageEnabled
public class GcsClientFactory {
    @SneakyThrows
    protected GoogleCredentials credentials(GcsConfig config) {
        GoogleCredentials credentials;

        if (config.getServiceAccount() != null) {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(config.getServiceAccount().getBytes());
            credentials = ServiceAccountCredentials.fromStream(byteArrayInputStream);

        } else {
            credentials = GoogleCredentials.getApplicationDefault();
        }

        return credentials;
    }

    @Singleton
    public Storage of(GcsConfig config) {
        return StorageOptions
            .newBuilder()
            .setCredentials(this.credentials(config))
            .setProjectId(config.getProjectId())
            .build()
            .getService();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy