use of software.amazon.awssdk.transfer.s3.CompletedFileUpload in project aws-sdk-java-v2 by aws.
the class UploadDirectoryHelper method uploadSingleFile.
private CompletableFuture<CompletedFileUpload> uploadSingleFile(UploadDirectoryRequest uploadDirectoryRequest, Collection<FailedFileUpload> failedFileUploads, Path path) {
int nameCount = uploadDirectoryRequest.sourceDirectory().getNameCount();
UploadFileRequest uploadFileRequest = constructUploadRequest(uploadDirectoryRequest, nameCount, path);
log.debug(() -> String.format("Sending upload request (%s) for path (%s)", uploadFileRequest, path));
CompletableFuture<CompletedFileUpload> future = uploadFunction.apply(uploadFileRequest).completionFuture();
future.whenComplete((r, t) -> {
if (t != null) {
failedFileUploads.add(FailedFileUpload.builder().exception(t).request(uploadFileRequest).build());
}
});
return future;
}
use of software.amazon.awssdk.transfer.s3.CompletedFileUpload in project aws-sdk-java-v2 by aws.
the class DefaultS3TransferManager method uploadFile.
@Override
public FileUpload uploadFile(UploadFileRequest uploadFileRequest) {
Validate.paramNotNull(uploadFileRequest, "uploadFileRequest");
AsyncRequestBody requestBody = AsyncRequestBody.fromFile(uploadFileRequest.source());
CompletableFuture<CompletedFileUpload> uploadFuture = new CompletableFuture<>();
TransferProgressUpdater progressUpdater = new TransferProgressUpdater(uploadFileRequest, requestBody);
progressUpdater.transferInitiated();
requestBody = progressUpdater.wrapRequestBody(requestBody);
progressUpdater.registerCompletion(uploadFuture);
try {
assertNotUnsupportedArn(uploadFileRequest.putObjectRequest().bucket(), "upload");
CompletableFuture<PutObjectResponse> putObjFuture = s3CrtAsyncClient.putObject(uploadFileRequest.putObjectRequest(), requestBody);
// Forward upload cancellation to CRT future
CompletableFutureUtils.forwardExceptionTo(uploadFuture, putObjFuture);
CompletableFutureUtils.forwardTransformedResultTo(putObjFuture, uploadFuture, r -> CompletedFileUpload.builder().response(r).build());
} catch (Throwable throwable) {
uploadFuture.completeExceptionally(throwable);
}
return new DefaultFileUpload(uploadFuture, progressUpdater.progress());
}
use of software.amazon.awssdk.transfer.s3.CompletedFileUpload in project aws-sdk-java-v2 by aws.
the class UploadDirectoryHelperTest method uploadDirectory_cancel_shouldCancelAllFutures.
@Test
public void uploadDirectory_cancel_shouldCancelAllFutures() {
CompletableFuture<CompletedFileUpload> future = new CompletableFuture<>();
FileUpload fileUpload = newUpload(future);
CompletableFuture<CompletedFileUpload> future2 = new CompletableFuture<>();
FileUpload fileUpload2 = newUpload(future2);
when(singleUploadFunction.apply(any(UploadFileRequest.class))).thenReturn(fileUpload, fileUpload2);
DirectoryUpload uploadDirectory = uploadDirectoryHelper.uploadDirectory(UploadDirectoryRequest.builder().sourceDirectory(directory).bucket("bucket").build());
uploadDirectory.completionFuture().cancel(true);
assertThatThrownBy(() -> future.get(1, TimeUnit.SECONDS)).isInstanceOf(CancellationException.class);
assertThatThrownBy(() -> future2.get(1, TimeUnit.SECONDS)).isInstanceOf(CancellationException.class);
}
use of software.amazon.awssdk.transfer.s3.CompletedFileUpload in project aws-sdk-java-v2 by aws.
the class UploadDirectoryHelperTest method uploadDirectory_allUploadsSucceed_failedUploadsShouldBeEmpty.
@Test
public void uploadDirectory_allUploadsSucceed_failedUploadsShouldBeEmpty() throws Exception {
PutObjectResponse putObjectResponse = PutObjectResponse.builder().eTag("1234").build();
CompletedFileUpload completedFileUpload = CompletedFileUpload.builder().response(putObjectResponse).build();
CompletableFuture<CompletedFileUpload> successfulFuture = new CompletableFuture<>();
FileUpload fileUpload = newUpload(successfulFuture);
successfulFuture.complete(completedFileUpload);
PutObjectResponse putObjectResponse2 = PutObjectResponse.builder().eTag("5678").build();
CompletedFileUpload completedFileUpload2 = CompletedFileUpload.builder().response(putObjectResponse2).build();
CompletableFuture<CompletedFileUpload> successfulFuture2 = new CompletableFuture<>();
FileUpload fileUpload2 = newUpload(successfulFuture2);
successfulFuture2.complete(completedFileUpload2);
when(singleUploadFunction.apply(any(UploadFileRequest.class))).thenReturn(fileUpload, fileUpload2);
DirectoryUpload uploadDirectory = uploadDirectoryHelper.uploadDirectory(UploadDirectoryRequest.builder().sourceDirectory(directory).bucket("bucket").build());
CompletedDirectoryUpload completedDirectoryUpload = uploadDirectory.completionFuture().get(5, TimeUnit.SECONDS);
assertThat(completedDirectoryUpload.failedTransfers()).isEmpty();
}
use of software.amazon.awssdk.transfer.s3.CompletedFileUpload in project aws-sdk-java-v2 by aws.
the class UploadDirectoryHelperTest method uploadDirectory_partialSuccess_shouldProvideFailedUploads.
@Test
public void uploadDirectory_partialSuccess_shouldProvideFailedUploads() throws Exception {
PutObjectResponse putObjectResponse = PutObjectResponse.builder().eTag("1234").build();
CompletedFileUpload completedFileUpload = CompletedFileUpload.builder().response(putObjectResponse).build();
CompletableFuture<CompletedFileUpload> successfulFuture = new CompletableFuture<>();
FileUpload fileUpload = newUpload(successfulFuture);
successfulFuture.complete(completedFileUpload);
SdkClientException exception = SdkClientException.create("failed");
CompletableFuture<CompletedFileUpload> failedFuture = new CompletableFuture<>();
FileUpload fileUpload2 = newUpload(failedFuture);
failedFuture.completeExceptionally(exception);
when(singleUploadFunction.apply(any(UploadFileRequest.class))).thenReturn(fileUpload, fileUpload2);
DirectoryUpload uploadDirectory = uploadDirectoryHelper.uploadDirectory(UploadDirectoryRequest.builder().sourceDirectory(directory).bucket("bucket").build());
CompletedDirectoryUpload completedDirectoryUpload = uploadDirectory.completionFuture().get(5, TimeUnit.SECONDS);
assertThat(completedDirectoryUpload.failedTransfers()).hasSize(1);
assertThat(completedDirectoryUpload.failedTransfers().iterator().next().exception()).isEqualTo(exception);
assertThat(completedDirectoryUpload.failedTransfers().iterator().next().request().source().toString()).isEqualTo("test/2");
}
Aggregations