1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.storage.v2.samples; 18 19 // [START storage_v2_generated_Storage_WriteObject_async] 20 import com.google.api.gax.rpc.ApiStreamObserver; 21 import com.google.storage.v2.CommonObjectRequestParams; 22 import com.google.storage.v2.ObjectChecksums; 23 import com.google.storage.v2.StorageClient; 24 import com.google.storage.v2.WriteObjectRequest; 25 import com.google.storage.v2.WriteObjectResponse; 26 27 public class AsyncWriteObject { 28 main(String[] args)29 public static void main(String[] args) throws Exception { 30 asyncWriteObject(); 31 } 32 asyncWriteObject()33 public static void asyncWriteObject() throws Exception { 34 // This snippet has been automatically generated and should be regarded as a code template only. 35 // It will require modifications to work: 36 // - It may require correct/in-range values for request initialization. 37 // - It may require specifying regional endpoints when creating the service client as shown in 38 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 39 try (StorageClient storageClient = StorageClient.create()) { 40 ApiStreamObserver<WriteObjectRequest> responseObserver = 41 new ApiStreamObserver<WriteObjectRequest>() { 42 @Override 43 public void onNext(WriteObjectResponse response) { 44 // Do something when a response is received. 45 } 46 47 @Override 48 public void onError(Throwable t) { 49 // Add error-handling 50 } 51 52 @Override 53 public void onCompleted() { 54 // Do something when complete. 55 } 56 }; 57 ApiStreamObserver<WriteObjectRequest> requestObserver = 58 storageClient.writeObject().clientStreamingCall(responseObserver); 59 WriteObjectRequest request = 60 WriteObjectRequest.newBuilder() 61 .setWriteOffset(-1559543565) 62 .setObjectChecksums(ObjectChecksums.newBuilder().build()) 63 .setFinishWrite(true) 64 .setCommonObjectRequestParams(CommonObjectRequestParams.newBuilder().build()) 65 .build(); 66 requestObserver.onNext(request); 67 } 68 } 69 } 70 // [END storage_v2_generated_Storage_WriteObject_async] 71