2020import jakarta .annotation .PreDestroy ;
2121import software .amazon .awssdk .auth .credentials .AwsBasicCredentials ;
2222import software .amazon .awssdk .auth .credentials .StaticCredentialsProvider ;
23+ import software .amazon .awssdk .http .SdkHttpClient ;
2324import software .amazon .awssdk .http .apache .ApacheHttpClient ;
2425import software .amazon .awssdk .http .apache .ProxyConfiguration ;
26+ import software .amazon .awssdk .http .async .SdkAsyncHttpClient ;
27+ import software .amazon .awssdk .http .crt .AwsCrtAsyncHttpClient ;
2528import software .amazon .awssdk .regions .Region ;
29+ import software .amazon .awssdk .services .s3 .S3AsyncClient ;
30+ import software .amazon .awssdk .services .s3 .S3AsyncClientBuilder ;
31+ import software .amazon .awssdk .services .s3 .S3BaseClientBuilder ;
2632import software .amazon .awssdk .services .s3 .S3Client ;
2733import software .amazon .awssdk .services .s3 .S3ClientBuilder ;
2834import software .amazon .awssdk .services .sts .StsClient ;
@@ -38,13 +44,33 @@ public final class S3FileSystemFactory
3844 implements TrinoFileSystemFactory
3945{
4046 private final S3Client client ;
47+ private final S3AsyncClient asyncClient ;
4148 private final S3Context context ;
4249
4350 @ Inject
4451 public S3FileSystemFactory (S3FileSystemConfig config )
4552 {
4653 S3ClientBuilder s3 = S3Client .builder ();
54+ applyS3Properties (s3 , config );
55+ s3 .httpClient (buildHttpClient (config ));
4756
57+ S3AsyncClientBuilder asyncS3 = S3AsyncClient .builder ();
58+ applyS3Properties (asyncS3 , config );
59+ asyncS3 .httpClient (buildAsyncHttpClient (config ));
60+
61+ this .client = s3 .build ();
62+ this .asyncClient = asyncS3 .build ();
63+
64+ context = new S3Context (
65+ toIntExact (config .getStreamingPartSize ().toBytes ()),
66+ config .isRequesterPays (),
67+ config .getSseType (),
68+ config .getSseKmsKeyId ());
69+
70+ }
71+
72+ private static void applyS3Properties (S3BaseClientBuilder <?, ?> s3 , S3FileSystemConfig config )
73+ {
4874 if ((config .getAwsAccessKey () != null ) && (config .getAwsSecretKey () != null )) {
4975 s3 .credentialsProvider (StaticCredentialsProvider .create (
5076 AwsBasicCredentials .create (config .getAwsAccessKey (), config .getAwsSecretKey ())));
@@ -70,7 +96,10 @@ public S3FileSystemFactory(S3FileSystemConfig config)
7096 .asyncCredentialUpdateEnabled (true )
7197 .build ());
7298 }
99+ }
73100
101+ private static SdkHttpClient buildHttpClient (S3FileSystemConfig config )
102+ {
74103 ApacheHttpClient .Builder httpClient = ApacheHttpClient .builder ()
75104 .maxConnections (config .getMaxConnections ());
76105
@@ -83,26 +112,34 @@ public S3FileSystemFactory(S3FileSystemConfig config)
83112 .build ());
84113 }
85114
86- s3 .httpClientBuilder (httpClient );
115+ return httpClient .build ();
116+ }
87117
88- this .client = s3 .build ();
118+ private static SdkAsyncHttpClient buildAsyncHttpClient (S3FileSystemConfig config )
119+ {
120+ AwsCrtAsyncHttpClient .Builder httpClient = AwsCrtAsyncHttpClient .builder ();
121+ if (config .getHttpProxy () != null ) {
122+ String scheme = config .isHttpProxySecure () ? "https" : "http" ;
123+ httpClient .proxyConfiguration (software .amazon .awssdk .http .crt .ProxyConfiguration .builder ()
124+ .scheme (scheme )
125+ .host (config .getHttpProxy ().getHost ())
126+ .port (config .getHttpProxy ().getPort ())
127+ .build ());
128+ }
89129
90- context = new S3Context (
91- toIntExact (config .getStreamingPartSize ().toBytes ()),
92- config .isRequesterPays (),
93- config .getSseType (),
94- config .getSseKmsKeyId ());
130+ return httpClient .build ();
95131 }
96132
97133 @ PreDestroy
98134 public void destroy ()
99135 {
100136 client .close ();
137+ asyncClient .close ();
101138 }
102139
103140 @ Override
104141 public TrinoFileSystem create (ConnectorIdentity identity )
105142 {
106- return new S3FileSystem (client , context );
143+ return new S3FileSystem (client , asyncClient , context );
107144 }
108145}
0 commit comments