.setProjectId(projectId) .setRetrySettings(retrySettings()) .setTransportOptions(transportOptions) .build(); return new RemoteStorageHelper(storageOptions);
private synchronized Storage getService() throws IOException, GeneralSecurityException { logger.trace("Getting Google Cloud Storage service"); // leave this here for tests because they do things like manipulate properties dynamically to test invalid values this.bucketName = properties.getProperty( "usergrid.binary.bucketname" ); if (instance == null) { // Google provides different authentication types which are different based on if the application is // running within GCE(Google Compute Engine) or GAE (Google App Engine). If Usergrid is running in // GCE or GAE, the SDK will automatically authenticate and get access to // cloud storage. Else, the full path to a credential file should be provided in the following environment variable // // GOOGLE_APPLICATION_CREDENTIALS // // The SDK will attempt to load the credential file for a service account. See the following // for more info: https://developers.google.com/identity/protocols/application-default-credentials#howtheywork GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(StorageScopes.all()); final TransportOptions transportOptions = HttpTransportOptions.newBuilder() .setConnectTimeout(30000) // in milliseconds .setReadTimeout(30000) // in milliseconds .build(); instance = StorageOptions.newBuilder() .setCredentials(credentials) .setTransportOptions(transportOptions) .build() .getService(); } return instance; }
@Test public void testInvalidTransport() { thrown.expect(IllegalArgumentException.class); StorageOptions.newBuilder().setTransportOptions(EasyMock.createMock(TransportOptions.class)); } }
.setProjectId(projectId) .setRetrySettings(retrySettings()) .setTransportOptions(transportOptions) .build(); return new RemoteStorageHelper(storageOptions);
/** * 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); }
@Test(timeout = 5000) public void testWriteChannelWithConnectionPool() throws IOException { TransportOptions transportOptions = HttpTransportOptions.newBuilder() .setHttpTransportFactory(new CustomHttpTransportFactory()) .build(); Storage storageWithPool = StorageOptions.newBuilder().setTransportOptions(transportOptions).build().getService(); String blobName = "test-custom-pool-management"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); byte[] stringBytes; try (WriteChannel writer = storageWithPool.writer(blob)) { stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8); writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT)); writer.write(ByteBuffer.wrap(stringBytes)); } try (WriteChannel writer = storageWithPool.writer(blob)) { stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8); writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT)); writer.write(ByteBuffer.wrap(stringBytes)); } }
@Override protected StorageOptions getServiceOptions(ProcessContext context, GoogleCredentials credentials) { final String projectId = context.getProperty(PROJECT_ID).evaluateAttributeExpressions().getValue(); final Integer retryCount = context.getProperty(RETRY_COUNT).asInteger(); StorageOptions.Builder storageOptionsBuilder = StorageOptions.newBuilder() .setCredentials(credentials) .setRetrySettings(RetrySettings.newBuilder() .setMaxAttempts(retryCount) .build()); if (!projectId.isEmpty()) { storageOptionsBuilder.setProjectId(projectId); } return storageOptionsBuilder.setTransportOptions(getTransportOptions(context)).build(); } }
/** * 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); }