// Path: kcbq-connector/src/main/java/com/wepay/kafka/connect/bigquery/exception/GCSConnectException.java
// public class GCSConnectException extends ConnectException {
//   public GCSConnectException(String msg) {
//     super(msg);
//   }
// 
//   public GCSConnectException(String msg, Throwable thr) {
//     super(msg, thr);
//   }
// }


import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.wepay.kafka.connect.bigquery.exception.GCSConnectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
    public Storage build() {
        return connect(projectName, key);
    }

    /**
     * Returns a default {@link Storage} instance for the specified project with credentials provided
     * in the specified file.
     *
     * @param projectName The name of the GCS project to work with
     * @param key The name of a file containing a JSON key that can be used to provide
     *                    credentials to GCS, or null if no authentication should be performed.
     * @return The resulting Storage object.
     */
    private Storage connect(String projectName, String key) {
        if (key == null) {
            return connect(projectName);
        }
        try {
            InputStream credentialsStream;
            if (keySource != null && keySource.equals("JSON")) {
                credentialsStream = new ByteArrayInputStream(key.getBytes(StandardCharsets.UTF_8));
            } else {
                credentialsStream = new FileInputStream(key);
            }
            return StorageOptions.newBuilder()
                .setProjectId(projectName)
                .setCredentials(GoogleCredentials.fromStream(credentialsStream))
                .build()
                .getService();
        } catch (IOException err) {
// Path: kcbq-connector/src/main/java/com/wepay/kafka/connect/bigquery/exception/GCSConnectException.java
// public class GCSConnectException extends ConnectException {
//   public GCSConnectException(String msg) {
//     super(msg);
//   }
// 
//   public GCSConnectException(String msg, Throwable thr) {
//     super(msg, thr);
//   }
// }


// Path: kcbq-connector/src/main/java/com/wepay/kafka/connect/bigquery/GCSBuilder.java
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.wepay.kafka.connect.bigquery.exception.GCSConnectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

    public Storage build() {
        return connect(projectName, key);
    }

    /**
     * Returns a default {@link Storage} instance for the specified project with credentials provided
     * in the specified file.
     *
     * @param projectName The name of the GCS project to work with
     * @param key The name of a file containing a JSON key that can be used to provide
     *                    credentials to GCS, or null if no authentication should be performed.
     * @return The resulting Storage object.
     */
    private Storage connect(String projectName, String key) {
        if (key == null) {
            return connect(projectName);
        }
        try {
            InputStream credentialsStream;
            if (keySource != null && keySource.equals("JSON")) {
                credentialsStream = new ByteArrayInputStream(key.getBytes(StandardCharsets.UTF_8));
            } else {
                credentialsStream = new FileInputStream(key);
            }
            return StorageOptions.newBuilder()
                .setProjectId(projectName)
                .setCredentials(GoogleCredentials.fromStream(credentialsStream))
                .build()
                .getService();
        } catch (IOException err) {
            throw new GCSConnectException("Failed to access json key file", err);