0
votes

I'm writing some bytes to gcs and would like to use the JSON API wrappers provided by Google, but with a timeout. Currently I have this:

storage = new Storage
            .Builder(GoogleNetHttpTransport...)
StorageObject storageObject = new StorageObject().setBucket(bucket).setName(path);
Storage.Objects.Insert insertObject =
                storage.objects().insert(bucket, storageObject, content).setName(path);
insertObject.execute();
    }

Is there a simple way to add a timeout to either CloudStorage, StorageObject or the .execute?

1

1 Answers

1
votes

It turns out that the storage abstraction import com.google.api.services.storage.Storage has a way to set timeouts on initialization with HttpRequestInitializers separate from your credentials.

If you have a MyGCSAbstraction that you create for each GCS operation, you can do the following:

private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer) {
    return new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
            requestInitializer.initialize(httpRequest);
            httpRequest.setConnectTimeout(1000); // ms
            httpRequest.setReadTimeout(1000); // ms
        }
    };
}

MyGCSAbstraction(String applicationName, Credential credential) throws GeneralSecurityException, IOException {
    Builder builder = new Storage.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), setHttpTimeout(credential));
    builder.setApplicationName(applicationName);
    storage = builder.build();
}