Search code examples
javagoogle-cloud-storagebatch-insert

Batch inserts in Google Cloud Storage using the Java API?


According to the documentation it's possible to batch inserts to the Google Cloud Storage service using HTTP. But how does one do that using the Java API? I cannot seem to find any documentation or examples of this.


Solution

  • According to the documentation, Google has provided a Google Cloud Client Library. The java doc of this library provides following sample code to make batch requests using a single RPC request:

    StorageBatch batch = storage.batch();
     BlobId firstBlob = BlobId.of("bucket", "blob1"));
     BlobId secondBlob = BlobId.of("bucket", "blob2"));
     batch.delete(firstBlob).notify(new BatchResult.Callback<Boolean, StorageException>() {
       public void success(Boolean result) {
         // deleted successfully
       }
    
       public void error(StorageException exception) {
         // delete failed
       }
     });
     batch.update(BlobInfo.builder(secondBlob).contentType("text/plain").build());
     StorageBatchResult<Blob> result = batch.get(secondBlob);
     batch.submit();
     Blob blob = result.get(); // returns get result or throws StorageException