2222import com .google .api .gax .paging .Page ;
2323import com .google .api .gax .retrying .RetrySettings ;
2424import com .google .api .gax .rpc .DeadlineExceededException ;
25+ import com .google .api .gax .rpc .TransportChannelProvider ;
2526import com .google .api .gax .rpc .UnavailableException ;
27+ import com .google .auth .Credentials ;
28+ import com .google .auth .oauth2 .GoogleCredentials ;
2629import com .google .cloud .ByteArray ;
2730import com .google .cloud .Date ;
31+ import com .google .cloud .NoCredentials ;
2832import com .google .cloud .Timestamp ;
2933import com .google .cloud .spanner .Backup ;
3034import com .google .cloud .spanner .BatchClient ;
144148import com .google .spanner .v1 .TypeCode ;
145149import io .grpc .Status ;
146150import io .grpc .stub .StreamObserver ;
151+ import java .io .ByteArrayInputStream ;
152+ import java .io .File ;
147153import java .io .IOException ;
148154import java .io .ObjectInputStream ;
149155import java .io .ObjectOutputStream ;
162168import java .util .logging .Logger ;
163169import java .util .stream .Collectors ;
164170import javax .annotation .Nullable ;
171+ import org .apache .commons .io .FileUtils ;
165172import org .jetbrains .annotations .NotNull ;
166173import org .threeten .bp .Duration ;
167174import org .threeten .bp .LocalDate ;
@@ -743,6 +750,20 @@ private synchronized Spanner getClient() throws IOException {
743750
744751 // Return the spanner client, create one if not exists.
745752 private synchronized Spanner getClient (long timeoutSeconds ) throws IOException {
753+ // Create a cloud spanner client
754+ Credentials credentials ;
755+ if (WorkerProxy .serviceKeyFile .isEmpty ()) {
756+ credentials = NoCredentials .getInstance ();
757+ } else {
758+ credentials =
759+ GoogleCredentials .fromStream (
760+ new ByteArrayInputStream (
761+ FileUtils .readFileToByteArray (new File (WorkerProxy .serviceKeyFile ))),
762+ HTTP_TRANSPORT_FACTORY );
763+ }
764+
765+ TransportChannelProvider channelProvider =
766+ CloudUtil .newChannelProviderHelper (WorkerProxy .spannerPort );
746767
747768 Duration rpcTimeout = Duration .ofHours (1L );
748769 if (timeoutSeconds > 0 ) {
@@ -761,7 +782,12 @@ private synchronized Spanner getClient(long timeoutSeconds) throws IOException {
761782
762783 // Cloud Spanner Client does not support global retry settings,
763784 // Thus, we need to add retry settings to each individual stub.
764- SpannerOptions .Builder optionsBuilder = SpannerOptions .newBuilder ().setProjectId (PROJECT_ID );
785+ SpannerOptions .Builder optionsBuilder =
786+ SpannerOptions .newBuilder ()
787+ .setProjectId (PROJECT_ID )
788+ .setHost (HOST_PREFIX + WorkerProxy .spannerPort )
789+ .setCredentials (credentials )
790+ .setChannelProvider (channelProvider );
765791
766792 SpannerStubSettings .Builder stubSettingsBuilder =
767793 optionsBuilder .getSpannerStubSettingsBuilder ();
0 commit comments