|
30 | 30 | import com.google.api.client.http.HttpHeaders; |
31 | 31 | import com.google.api.client.http.HttpRequestInitializer; |
32 | 32 | import com.google.api.client.http.HttpStatusCodes; |
33 | | -import com.google.api.client.http.HttpTransport; |
34 | 33 | import com.google.api.client.util.BackOff; |
35 | 34 | import com.google.api.client.util.Sleeper; |
36 | 35 | import com.google.api.services.storage.Storage; |
|
53 | 52 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
54 | 53 | import java.io.FileNotFoundException; |
55 | 54 | import java.io.IOException; |
56 | | -import java.lang.reflect.Method; |
57 | 55 | import java.nio.channels.SeekableByteChannel; |
58 | 56 | import java.nio.channels.WritableByteChannel; |
59 | 57 | import java.nio.file.AccessDeniedException; |
@@ -267,8 +265,12 @@ public boolean shouldRetry(IOException e) { |
267 | 265 | .setReadChannelOptions(gcsReadOptions) |
268 | 266 | .setGrpcEnabled(shouldUseGrpc) |
269 | 267 | .build(); |
270 | | - googleCloudStorage = |
271 | | - createGoogleCloudStorage(googleCloudStorageOptions, storageClient, credentials); |
| 268 | + try { |
| 269 | + googleCloudStorage = |
| 270 | + createGoogleCloudStorage(googleCloudStorageOptions, storageClient, credentials); |
| 271 | + } catch (IOException e) { |
| 272 | + throw new RuntimeException(e); |
| 273 | + } |
272 | 274 | this.batchRequestSupplier = |
273 | 275 | () -> { |
274 | 276 | // Capture reference to this so that the most recent storageClient and initializer |
@@ -725,48 +727,16 @@ public WritableByteChannel create(GcsPath path, CreateOptions options) throws IO |
725 | 727 | } |
726 | 728 |
|
727 | 729 | GoogleCloudStorage createGoogleCloudStorage( |
728 | | - GoogleCloudStorageOptions options, Storage storage, Credentials credentials) { |
729 | | - try { |
730 | | - return new GoogleCloudStorageImpl(options, storage, credentials); |
731 | | - } catch (NoSuchMethodError e) { |
732 | | - // gcs-connector 3.x drops the direct constructor and exclusively uses Builder |
733 | | - // TODO eliminate reflection once Beam drops Java 8 support and upgrades to gcsio 3.x |
734 | | - try { |
735 | | - final Method builderMethod = GoogleCloudStorageImpl.class.getMethod("builder"); |
736 | | - Object builder = builderMethod.invoke(null); |
737 | | - final Class<?> builderClass = |
738 | | - Class.forName( |
739 | | - "com.google.cloud.hadoop.gcsio.AutoBuilder_GoogleCloudStorageImpl_Builder"); |
740 | | - |
741 | | - final Method setOptionsMethod = |
742 | | - builderClass.getMethod("setOptions", GoogleCloudStorageOptions.class); |
743 | | - setOptionsMethod.setAccessible(true); |
744 | | - builder = setOptionsMethod.invoke(builder, options); |
745 | | - |
746 | | - final Method setHttpTransportMethod = |
747 | | - builderClass.getMethod("setHttpTransport", HttpTransport.class); |
748 | | - setHttpTransportMethod.setAccessible(true); |
749 | | - builder = |
750 | | - setHttpTransportMethod.invoke(builder, storage.getRequestFactory().getTransport()); |
751 | | - |
752 | | - final Method setCredentialsMethod = |
753 | | - builderClass.getMethod("setCredentials", Credentials.class); |
754 | | - setCredentialsMethod.setAccessible(true); |
755 | | - builder = setCredentialsMethod.invoke(builder, credentials); |
756 | | - |
757 | | - final Method setHttpRequestInitializerMethod = |
758 | | - builderClass.getMethod("setHttpRequestInitializer", HttpRequestInitializer.class); |
759 | | - setHttpRequestInitializerMethod.setAccessible(true); |
760 | | - builder = setHttpRequestInitializerMethod.invoke(builder, httpRequestInitializer); |
761 | | - |
762 | | - final Method buildMethod = builderClass.getMethod("build"); |
763 | | - buildMethod.setAccessible(true); |
764 | | - return (GoogleCloudStorage) buildMethod.invoke(builder); |
765 | | - } catch (Exception reflectionError) { |
766 | | - throw new RuntimeException( |
767 | | - "Failed to construct GoogleCloudStorageImpl from gcsio 3.x Builder", reflectionError); |
768 | | - } |
769 | | - } |
| 730 | + GoogleCloudStorageOptions options, Storage storage, Credentials credentials) |
| 731 | + throws IOException { |
| 732 | + return GoogleCloudStorageImpl.builder() |
| 733 | + .setOptions(options) |
| 734 | + .setHttpTransport(storage.getRequestFactory().getTransport()) |
| 735 | + .setCredentials(credentials) |
| 736 | + // gcsio 3 expects httpRequestInitializer to be either absent or |
| 737 | + // com.google.cloud.hadoop.util.RetryHttpInitializer when credentials not provided |
| 738 | + .setHttpRequestInitializer(credentials != null ? httpRequestInitializer : null) |
| 739 | + .build(); |
770 | 740 | } |
771 | 741 |
|
772 | 742 | /** |
|
0 commit comments