1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 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 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.services.s3; 17 18 import java.net.URI; 19 import java.nio.file.Path; 20 import java.util.concurrent.CompletableFuture; 21 import java.util.concurrent.Executor; 22 import java.util.concurrent.ThreadPoolExecutor; 23 import java.util.function.Consumer; 24 import software.amazon.awssdk.annotations.SdkPublicApi; 25 import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; 26 import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; 27 import software.amazon.awssdk.identity.spi.IdentityProvider; 28 import software.amazon.awssdk.regions.Region; 29 import software.amazon.awssdk.services.s3.crt.S3CrtHttpConfiguration; 30 import software.amazon.awssdk.services.s3.crt.S3CrtRetryConfiguration; 31 import software.amazon.awssdk.services.s3.model.GetObjectRequest; 32 import software.amazon.awssdk.services.s3.model.PutObjectRequest; 33 import software.amazon.awssdk.utils.Validate; 34 import software.amazon.awssdk.utils.builder.SdkBuilder; 35 36 /** 37 * Builder API to build instance of Common Run Time based S3AsyncClient. 38 */ 39 @SdkPublicApi 40 public interface S3CrtAsyncClientBuilder extends SdkBuilder<S3CrtAsyncClientBuilder, S3AsyncClient> { 41 42 /** 43 * Configure the credentials that should be used to authenticate with S3. 44 * 45 * <p>The default provider will attempt to identify the credentials automatically using the following checks: 46 * <ol> 47 * <li>Java System Properties - <code>aws.accessKeyId</code> and <code>aws.secretKey</code></li> 48 * <li>Environment Variables - <code>AWS_ACCESS_KEY_ID</code> and <code>AWS_SECRET_ACCESS_KEY</code></li> 49 * <li>Credential profiles file at the default location (~/.aws/credentials) shared by all AWS SDKs and the AWS CLI</li> 50 * <li>Credentials delivered through the Amazon EC2 container service if AWS_CONTAINER_CREDENTIALS_RELATIVE_URI 51 * environment variable is set and security manager has permission to access the variable.</li> 52 * <li>Instance profile credentials delivered through the Amazon EC2 metadata service</li> 53 * </ol> 54 * 55 * <p>If the credentials are not found in any of the locations above, an exception will be thrown at {@link #build()} 56 * time. 57 * </p> 58 * 59 * @param credentialsProvider the credentials to use 60 * @return This builder for method chaining. 61 */ credentialsProvider(AwsCredentialsProvider credentialsProvider)62 default S3CrtAsyncClientBuilder credentialsProvider(AwsCredentialsProvider credentialsProvider) { 63 return credentialsProvider((IdentityProvider<? extends AwsCredentialsIdentity>) credentialsProvider); 64 } 65 66 /** 67 * Configure the credentials that should be used to authenticate with S3. 68 * 69 * <p>The default provider will attempt to identify the credentials automatically using the following checks: 70 * <ol> 71 * <li>Java System Properties - {@code aws.accessKeyId} and {@code aws.secretKey}</li> 72 * <li>Environment Variables - {@code AWS_ACCESS_KEY_ID} and {@code AWS_SECRET_ACCESS_KEY}</li> 73 * <li>Credential profiles file at the default location (~/.aws/credentials) shared by all AWS SDKs and the AWS CLI</li> 74 * <li>Credentials delivered through the Amazon EC2 container service if AWS_CONTAINER_CREDENTIALS_RELATIVE_URI 75 * environment variable is set and security manager has permission to access the variable.</li> 76 * <li>Instance profile credentials delivered through the Amazon EC2 metadata service</li> 77 * </ol> 78 * 79 * <p>If the credentials are not found in any of the locations above, an exception will be thrown at {@link #build()} 80 * time. 81 * </p> 82 * 83 * @param credentialsProvider the credentials to use 84 * @return This builder for method chaining. 85 */ credentialsProvider(IdentityProvider<? extends AwsCredentialsIdentity> credentialsProvider)86 default S3CrtAsyncClientBuilder credentialsProvider(IdentityProvider<? extends AwsCredentialsIdentity> credentialsProvider) { 87 throw new UnsupportedOperationException(); 88 } 89 90 /** 91 * Configure the region with which the SDK should communicate. 92 * 93 * <p>If this is not specified, the SDK will attempt to identify the endpoint automatically using the following logic: 94 * <ol> 95 * <li>Check the 'aws.region' system property for the region.</li> 96 * <li>Check the 'AWS_REGION' environment variable for the region.</li> 97 * <li>Check the {user.home}/.aws/credentials and {user.home}/.aws/config files for the region.</li> 98 * <li>If running in EC2, check the EC2 metadata service for the region.</li> 99 * </ol> 100 * 101 * @param region the region to be used 102 * @return this builder for method chaining. 103 */ region(Region region)104 S3CrtAsyncClientBuilder region(Region region); 105 106 /** 107 * Sets the minimum part size for transfer parts. Decreasing the minimum part size causes multipart transfer to be split into 108 * a larger number of smaller parts. Setting this value too low has a negative effect on transfer speeds, causing extra 109 * latency and network communication for each part. 110 * 111 * <p> 112 * By default, it is 8MB. See <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/qfacts.html">Amazon S3 multipart 113 * upload limits</a> for guidance. 114 * 115 * @param uploadPartSize The minimum part size for transfer parts. 116 * @return this builder for method chaining. 117 */ minimumPartSizeInBytes(Long uploadPartSize)118 S3CrtAsyncClientBuilder minimumPartSizeInBytes(Long uploadPartSize); 119 120 /** 121 * The amount of native memory that CRT is allowed to use when making requests to S3. 122 * <p> 123 * If not provided, the CRT attempts to limit native memory usage in an optimal way, based on a number of parameters 124 * such as target throughput. Therefore, only configure the memory limit explicitly when needed. 125 * <p> 126 * Supported range: 127 * <ul> 128 * <li><b>Min: </b>1 GB</li> 129 * <li><b>Max: </b>The lowest value of the supplied value and the SIZE_MAX of the system</li> 130 * </ul> 131 * 132 * @param maxNativeMemoryLimitInBytes 133 the native memory limit in bytes 134 * @return this builder for method chaining. 135 * @see #targetThroughputInGbps(Double) 136 */ maxNativeMemoryLimitInBytes(Long maxNativeMemoryLimitInBytes )137 S3CrtAsyncClientBuilder maxNativeMemoryLimitInBytes(Long maxNativeMemoryLimitInBytes 138 ); 139 140 /** 141 * The target throughput for transfer requests. Higher value means more connections will be established with S3. 142 * 143 * <p> 144 * Whether the transfer manager can achieve the configured target throughput depends on various factors such as the network 145 * bandwidth of the environment, whether {@link #maxConcurrency} is configured and amount of available memory. 146 * 147 * <p> 148 * By default, it is 10 gigabits per second. If users want to transfer as fast as possible, it's recommended to set it to the 149 * maximum network bandwidth on the host that the application is running on. For EC2 instances, you can find network 150 * bandwidth for a specific 151 * instance type in <a href="https://aws.amazon.com/ec2/instance-types/">Amazon EC2 instance type page</a>. 152 * If you are running into out of file descriptors error, consider using {@link #maxConcurrency(Integer)} to limit the 153 * number of connections. 154 * <p> 155 * <b>Note: </b> This setting affects the native memory usage used by CRT; a higher throughput value will result in a larger 156 * memory usage. Typically, a range of throughput values maps to a discrete memory limit value in CRT, with a maximum upper 157 * limit. 158 * 159 * @param targetThroughputInGbps the target throughput in Gbps 160 * @return this builder for method chaining. 161 * @see #maxConcurrency(Integer) 162 * @see #maxNativeMemoryLimitInBytes(Long) 163 */ targetThroughputInGbps(Double targetThroughputInGbps)164 S3CrtAsyncClientBuilder targetThroughputInGbps(Double targetThroughputInGbps); 165 166 /** 167 * Specifies the maximum number of S3 connections that should be established during 168 * a transfer. 169 * 170 * <p> 171 * If not provided, the TransferManager will calculate the optional number of connections 172 * based on {@link #targetThroughputInGbps}. If the value is too low, the S3TransferManager 173 * might not achieve the specified target throughput. 174 * 175 * @param maxConcurrency the max number of concurrent requests 176 * @return this builder for method chaining. 177 * @see #targetThroughputInGbps(Double) 178 */ maxConcurrency(Integer maxConcurrency)179 S3CrtAsyncClientBuilder maxConcurrency(Integer maxConcurrency); 180 181 /** 182 * Configure the endpoint override with which the SDK should communicate. 183 * 184 * @param endpointOverride the endpoint override to be used 185 * @return this builder for method chaining. 186 */ endpointOverride(URI endpointOverride)187 S3CrtAsyncClientBuilder endpointOverride(URI endpointOverride); 188 189 /** 190 * Option to disable checksum validation for {@link S3AsyncClient#getObject(GetObjectRequest, Path)} and 191 * {@link S3AsyncClient#putObject(PutObjectRequest, Path)}. 192 * 193 * <p> 194 * Checksum validation using CRC32 is enabled by default. 195 */ checksumValidationEnabled(Boolean checksumValidationEnabled)196 S3CrtAsyncClientBuilder checksumValidationEnabled(Boolean checksumValidationEnabled); 197 198 /** 199 * Configure the starting buffer size the client will use to buffer the parts downloaded from S3. Maintain a larger window to 200 * keep up a high download throughput; parts cannot download in parallel unless the window is large enough to hold multiple 201 * parts. Maintain a smaller window to limit the amount of data buffered in memory. 202 * 203 * <p> 204 * By default, it is equal to the resolved part size * 10 205 * 206 * @param initialReadBufferSizeInBytes the initial read buffer size 207 * @return this builder for method chaining. 208 */ initialReadBufferSizeInBytes(Long initialReadBufferSizeInBytes)209 S3CrtAsyncClientBuilder initialReadBufferSizeInBytes(Long initialReadBufferSizeInBytes); 210 211 212 /** 213 * Sets the HTTP configuration to use for this client. 214 * 215 * @param configuration The http proxy configuration to use 216 * @return The builder of the method chaining. 217 */ httpConfiguration(S3CrtHttpConfiguration configuration)218 S3CrtAsyncClientBuilder httpConfiguration(S3CrtHttpConfiguration configuration); 219 220 /** 221 * Sets the Retry configuration to use for this client. 222 * 223 * @param retryConfiguration The retry configurations to be used. 224 * @return The builder of the method chaining. 225 */ retryConfiguration(S3CrtRetryConfiguration retryConfiguration)226 S3CrtAsyncClientBuilder retryConfiguration(S3CrtRetryConfiguration retryConfiguration); 227 228 /** 229 * A convenience method that creates an instance of the {@link S3CrtHttpConfiguration} builder, avoiding the 230 * need to create one manually via {@link S3CrtHttpConfiguration#builder()}. 231 * 232 * @param configurationBuilder The health checks config builder to use 233 * @return The builder of the method chaining. 234 * @see #httpConfiguration(S3CrtHttpConfiguration) 235 */ httpConfiguration(Consumer<S3CrtHttpConfiguration.Builder> configurationBuilder)236 default S3CrtAsyncClientBuilder httpConfiguration(Consumer<S3CrtHttpConfiguration.Builder> configurationBuilder) { 237 Validate.paramNotNull(configurationBuilder, "configurationBuilder"); 238 return httpConfiguration(S3CrtHttpConfiguration.builder() 239 .applyMutation(configurationBuilder) 240 .build()); 241 } 242 243 // S3 client context params, copied from S3BaseClientBuilder. Note we only have accelerate and path style because they're 244 // the only ones we can support in the CRT client (does not affect signing). 245 /** 246 * Enables this client to use S3 Transfer Acceleration endpoints. 247 */ accelerate(Boolean accelerate)248 S3CrtAsyncClientBuilder accelerate(Boolean accelerate); 249 250 /** 251 * Forces this client to use path-style addressing for buckets. 252 */ forcePathStyle(Boolean forcePathStyle)253 S3CrtAsyncClientBuilder forcePathStyle(Boolean forcePathStyle); 254 255 /** 256 * A convenience method that creates an instance of the {@link S3CrtRetryConfiguration} builder, avoiding the 257 * need to create one manually via {@link S3CrtRetryConfiguration#builder()}. 258 * 259 * @param retryConfigurationBuilder The retry config builder to use 260 * @return The builder of the method chaining. 261 * @see #retryConfiguration(S3CrtRetryConfiguration) 262 */ retryConfiguration(Consumer<S3CrtRetryConfiguration.Builder> retryConfigurationBuilder)263 default S3CrtAsyncClientBuilder retryConfiguration(Consumer<S3CrtRetryConfiguration.Builder> retryConfigurationBuilder) { 264 Validate.paramNotNull(retryConfigurationBuilder, "retryConfigurationBuilder"); 265 return retryConfiguration(S3CrtRetryConfiguration.builder() 266 .applyMutation(retryConfigurationBuilder) 267 .build()); 268 } 269 270 /** 271 * <p> Configures whether cross-region bucket access is enabled for clients using the configuration. 272 * <p>The following behavior is used when this mode is enabled: 273 * <ol> 274 * <li>This method allows enabling or disabling cross-region bucket access for clients. When cross-region bucket 275 * access is enabled, requests that do not act on an existing bucket (e.g., createBucket API) will be routed to the 276 * region configured on the client</li> 277 * <li>The first time a request is made that references an existing bucket (e.g., putObject API), a request will be 278 * made to the client-configured region. If the bucket does not exist in this region, the service will include the 279 * actual region in the error responses. Subsequently, the API will be called using the correct region obtained 280 * from the error response. </li> 281 * <li>This location may be cached in the client for subsequent requests to the same bucket.</li> 282 * </ol> 283 * <p>Enabling this mode has several drawbacks, as it can increase latency if the bucket's location is physically far 284 * from the location of the request.Therefore, it is strongly advised, whenever possible, to know the location of your 285 * buckets and create a region-specific client to access them 286 * 287 * @param crossRegionAccessEnabled Whether cross region bucket access should be enabled. 288 * @return The builder object for method chaining. 289 */ crossRegionAccessEnabled(Boolean crossRegionAccessEnabled)290 S3CrtAsyncClientBuilder crossRegionAccessEnabled(Boolean crossRegionAccessEnabled); 291 292 /** 293 * Configure the size threshold, in bytes, for when to use multipart upload. Uploads/copies over this size will automatically 294 * use a multipart upload strategy, while uploads/copies smaller than this threshold will use a single connection to 295 * upload/copy the whole object. 296 * 297 * <p> 298 * Multipart uploads are easier to recover from and also potentially faster than single part uploads, especially when the 299 * upload parts can be uploaded in parallel. Because there are additional network API calls, small objects are still 300 * recommended to use a single connection for the upload. See 301 * <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/mpuoverview.html">Uploading and copying objects using 302 * multipart upload</a>. 303 * 304 * <p> 305 * By default, it is the same as {@link #minimumPartSizeInBytes(Long)}. 306 * 307 * @param thresholdInBytes the value of the threshold to set. 308 * @return an instance of this builder. 309 */ thresholdInBytes(Long thresholdInBytes)310 S3CrtAsyncClientBuilder thresholdInBytes(Long thresholdInBytes); 311 312 /** 313 * Configure the {@link Executor} that should be used to complete the {@link CompletableFuture} that is returned by the async 314 * service client. By default, this is a dedicated, per-client {@link ThreadPoolExecutor} that is managed by the SDK. 315 * <p> 316 * The configured {@link Executor} will be invoked by the async HTTP client's I/O threads (e.g., EventLoops), which must be 317 * reserved for non-blocking behavior. Blocking an I/O thread can cause severe performance degradation, including across 318 * multiple clients, as clients are configured, by default, to share a single I/O thread pool (e.g., EventLoopGroup). 319 * <p> 320 * You should typically only want to customize the future-completion {@link Executor} for a few possible reasons: 321 * <ol> 322 * <li>You want more fine-grained control over the {@link ThreadPoolExecutor} used, such as configuring the pool size 323 * or sharing a single pool between multiple clients. 324 * <li>You want to add instrumentation (i.e., metrics) around how the {@link Executor} is used. 325 * <li>You know, for certain, that all of your {@link CompletableFuture} usage is strictly non-blocking, and you wish to 326 * remove the minor overhead incurred by using a separate thread. In this case, you can use 327 * {@code Runnable::run} to execute the future-completion directly from within the I/O thread. 328 * </ol> 329 * 330 * @param futureCompletionExecutor the executor 331 * @return an instance of this builder. 332 */ futureCompletionExecutor(Executor futureCompletionExecutor)333 S3CrtAsyncClientBuilder futureCompletionExecutor(Executor futureCompletionExecutor); 334 335 336 @Override build()337 S3AsyncClient build(); 338 }