Is there a way to effectively cancel or abort an on going download from the AWS Java SDK v2 S3TransferManager
? Looks like there are a few ways to pause and resume downloads, but no way to abort the download entirely. (?)
Context: SpringBoot application downloading data from S3 to on-prem using the AWS Java SDK v2 with the S3TransferManager
configured with multipart transfers using an S3AsyncClient
with temporal credentials.
Need: I need to be able to cancel certain downloads that are taking more time than a give threshold.
- Client configuration
SdkAsyncHttpClient httpClient = AwsCrtAsyncHttpClient.builder()
.maxConcurrency(s3ClientMaxConnections)
.connectionTimeout(Duration.ofMillis(connectionTimeOut))
.connectionMaxIdleTime(Duration.ofMillis(socketTimeOut))
.build();
KeyGeneratorResponse keys = objectMapper.readValue(
restResponse.getResponseData().get(),
KeyGeneratorResponse.class);
StaticCredentialsProvider staticCredentialsProvider = StaticCredentialsProvider
.create(AwsSessionCredentials.create(keys.getAccessKey(),
keys.getSecretKey(), keys.getSessionToken()));
MultipartConfiguration multipartConfiguration = MultipartConfiguration
.builder().thresholdInBytes(this.multipartThreshold)
.minimumPartSizeInBytes(this.minimumUploadPartSize).build();
S3AsyncClient s3AsyncClient = S3AsyncClient.builder()
.httpClient(httpClient).multipartEnabled(true)
.multipartConfiguration(multipartConfiguration)
.region(Region.of(region))
.credentialsProvider(staticCredentialsProvider)
.multipartEnabled(true).build();
......
.....
this.transferManager = S3TransferManager.builder().s3Client(this.s3Client)
.executor(Executors.newFixedThreadPool(this.transferManagerThreadPoolSize))
.build();
- Download code:
DownloadFileRequest downloadFileRequest = DownloadFileRequest.builder()
.addTransferListener(LoggingTransferListener.create())
.getObjectRequest(req -> req.bucket(bucket).key(key)).destination(localFile)
.build();
FileDownload download = s3Config.getTransferManager().downloadFile(downloadFileRequest);
CompletedFileDownload downloadResult = download.completionFuture().join();