Search in sources :

Example 1 with StorageOptions

use of com.google.cloud.storage.StorageOptions in project google-cloud-java by GoogleCloudPlatform.

the class RemoteStorageHelper method create.

/**
   * Creates a {@code RemoteStorageHelper} object for the given project id and JSON key input
   * stream.
   *
   * @param projectId id of the project to be used for running the tests
   * @param keyStream input stream for a JSON key
   * @return A {@code RemoteStorageHelper} object for the provided options
   * @throws com.google.cloud.storage.testing.RemoteStorageHelper.StorageHelperException if
   *     {@code keyStream} is not a valid JSON key stream
   */
public static RemoteStorageHelper create(String projectId, InputStream keyStream) throws StorageHelperException {
    try {
        HttpTransportOptions transportOptions = StorageOptions.getDefaultHttpTransportOptions();
        transportOptions = transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000).build();
        StorageOptions storageOptions = StorageOptions.newBuilder().setCredentials(GoogleCredentials.fromStream(keyStream)).setProjectId(projectId).setRetrySettings(retrySettings()).setTransportOptions(transportOptions).build();
        return new RemoteStorageHelper(storageOptions);
    } catch (IOException ex) {
        if (log.isLoggable(Level.WARNING)) {
            log.log(Level.WARNING, ex.getMessage());
        }
        throw StorageHelperException.translate(ex);
    }
}
Also used : StorageOptions(com.google.cloud.storage.StorageOptions) IOException(java.io.IOException) HttpTransportOptions(com.google.cloud.http.HttpTransportOptions)

Example 2 with StorageOptions

use of com.google.cloud.storage.StorageOptions in project google-cloud-java by GoogleCloudPlatform.

the class RemoteStorageHelper method create.

/**
   * Creates a {@code RemoteStorageHelper} object using default project id and authentication
   * credentials.
   */
public static RemoteStorageHelper create() throws StorageHelperException {
    HttpTransportOptions transportOptions = StorageOptions.getDefaultHttpTransportOptions();
    transportOptions = transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000).build();
    StorageOptions storageOptions = StorageOptions.newBuilder().setRetrySettings(retrySettings()).setTransportOptions(transportOptions).build();
    return new RemoteStorageHelper(storageOptions);
}
Also used : StorageOptions(com.google.cloud.storage.StorageOptions) HttpTransportOptions(com.google.cloud.http.HttpTransportOptions)

Example 3 with StorageOptions

use of com.google.cloud.storage.StorageOptions in project google-cloud-java by GoogleCloudPlatform.

the class RemoteStorageHelperTest method testCreateFromStream.

@Test
public void testCreateFromStream() {
    RemoteStorageHelper helper = RemoteStorageHelper.create(PROJECT_ID, JSON_KEY_STREAM);
    StorageOptions options = helper.getOptions();
    assertEquals(PROJECT_ID, options.getProjectId());
    assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getConnectTimeout());
    assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getReadTimeout());
    assertEquals(10, options.getRetrySettings().getMaxAttempts());
    assertEquals(Duration.ofMillis(30000), options.getRetrySettings().getMaxRetryDelay());
    assertEquals(Duration.ofMillis(120000), options.getRetrySettings().getTotalTimeout());
    assertEquals(Duration.ofMillis(250), options.getRetrySettings().getInitialRetryDelay());
}
Also used : StorageOptions(com.google.cloud.storage.StorageOptions) Test(org.junit.Test)

Example 4 with StorageOptions

use of com.google.cloud.storage.StorageOptions in project gatk by broadinstitute.

the class BucketUtils method getPathOnGcs.

/**
     * String -> Path. This *should* not be necessary (use Paths.get(URI.create(...)) instead) , but it currently is
     * on Spark because using the fat, shaded jar breaks the registration of the GCS FilesystemProvider.
     * To transform other types of string URLs into Paths, use IOUtils.getPath instead.
     */
public static java.nio.file.Path getPathOnGcs(String gcsUrl) {
    // use a split limit of -1 to preserve empty split tokens, especially trailing slashes on directory names
    final String[] split = gcsUrl.split("/", -1);
    final String BUCKET = split[2];
    final String pathWithoutBucket = String.join("/", Arrays.copyOfRange(split, 3, split.length));
    CloudStorageConfiguration cloudConfig = CloudStorageConfiguration.builder().maxChannelReopens(NIO_MAX_REOPENS).build();
    StorageOptions sopt = setGenerousTimeouts(StorageOptions.newBuilder()).build();
    return CloudStorageFileSystem.forBucket(BUCKET, cloudConfig, sopt).getPath(pathWithoutBucket);
}
Also used : StorageOptions(com.google.cloud.storage.StorageOptions) CloudStorageConfiguration(com.google.cloud.storage.contrib.nio.CloudStorageConfiguration)

Example 5 with StorageOptions

use of com.google.cloud.storage.StorageOptions in project gatk by broadinstitute.

the class BucketUtils method getAuthenticatedGcs.

/**
     * Get an authenticated GCS-backed NIO FileSystem object representing the selected projected and bucket.
     * Credentials are found automatically when running on Compute/App engine, logged into gcloud, or
     * if the GOOGLE_APPLICATION_CREDENTIALS env. variable is set. In that case leave credentials null.
     * Otherwise, you must pass the contents of the service account credentials file.
     * See https://github.com/GoogleCloudPlatform/gcloud-java#authentication
     *
     * Note that most of the time it's enough to just open a file via
     * Files.newInputStream(Paths.get(URI.create( path ))).
     **/
public static java.nio.file.FileSystem getAuthenticatedGcs(String projectId, String bucket, byte[] credentials) throws IOException {
    StorageOptions.Builder builder = StorageOptions.newBuilder().setProjectId(projectId);
    if (null != credentials) {
        builder = builder.setCredentials(GoogleCredentials.fromStream(new ByteArrayInputStream(credentials)));
    }
    // generous timeouts, to avoid tests failing when not warranted.
    StorageOptions storageOptions = setGenerousTimeouts(builder).build();
    // 2. Create GCS filesystem object with those credentials
    return CloudStorageFileSystem.forBucket(bucket, CloudStorageConfiguration.DEFAULT, storageOptions);
}
Also used : StorageOptions(com.google.cloud.storage.StorageOptions)

Aggregations

StorageOptions (com.google.cloud.storage.StorageOptions)5 HttpTransportOptions (com.google.cloud.http.HttpTransportOptions)2 CloudStorageConfiguration (com.google.cloud.storage.contrib.nio.CloudStorageConfiguration)1 IOException (java.io.IOException)1 Test (org.junit.Test)1