Search in sources :

Example 1 with CompletedFileUpload

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;
}
Also used : UploadFileRequest(software.amazon.awssdk.transfer.s3.UploadFileRequest) CompletedFileUpload(software.amazon.awssdk.transfer.s3.CompletedFileUpload)

Example 2 with CompletedFileUpload

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());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CompletedFileUpload(software.amazon.awssdk.transfer.s3.CompletedFileUpload) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) TransferProgressUpdater(software.amazon.awssdk.transfer.s3.internal.progress.TransferProgressUpdater) AsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody)

Example 3 with CompletedFileUpload

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);
}
Also used : UploadFileRequest(software.amazon.awssdk.transfer.s3.UploadFileRequest) CompletableFuture(java.util.concurrent.CompletableFuture) CompletedFileUpload(software.amazon.awssdk.transfer.s3.CompletedFileUpload) CompletedDirectoryUpload(software.amazon.awssdk.transfer.s3.CompletedDirectoryUpload) DirectoryUpload(software.amazon.awssdk.transfer.s3.DirectoryUpload) FileUpload(software.amazon.awssdk.transfer.s3.FileUpload) CompletedFileUpload(software.amazon.awssdk.transfer.s3.CompletedFileUpload) Test(org.junit.jupiter.api.Test)

Example 4 with CompletedFileUpload

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();
}
Also used : UploadFileRequest(software.amazon.awssdk.transfer.s3.UploadFileRequest) CompletedDirectoryUpload(software.amazon.awssdk.transfer.s3.CompletedDirectoryUpload) CompletableFuture(java.util.concurrent.CompletableFuture) CompletedFileUpload(software.amazon.awssdk.transfer.s3.CompletedFileUpload) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) CompletedDirectoryUpload(software.amazon.awssdk.transfer.s3.CompletedDirectoryUpload) DirectoryUpload(software.amazon.awssdk.transfer.s3.DirectoryUpload) FileUpload(software.amazon.awssdk.transfer.s3.FileUpload) CompletedFileUpload(software.amazon.awssdk.transfer.s3.CompletedFileUpload) Test(org.junit.jupiter.api.Test)

Example 5 with CompletedFileUpload

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");
}
Also used : UploadFileRequest(software.amazon.awssdk.transfer.s3.UploadFileRequest) CompletedDirectoryUpload(software.amazon.awssdk.transfer.s3.CompletedDirectoryUpload) CompletableFuture(java.util.concurrent.CompletableFuture) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) CompletedFileUpload(software.amazon.awssdk.transfer.s3.CompletedFileUpload) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) CompletedDirectoryUpload(software.amazon.awssdk.transfer.s3.CompletedDirectoryUpload) DirectoryUpload(software.amazon.awssdk.transfer.s3.DirectoryUpload) FileUpload(software.amazon.awssdk.transfer.s3.FileUpload) CompletedFileUpload(software.amazon.awssdk.transfer.s3.CompletedFileUpload) Test(org.junit.jupiter.api.Test)

Aggregations

CompletedFileUpload (software.amazon.awssdk.transfer.s3.CompletedFileUpload)9 CompletableFuture (java.util.concurrent.CompletableFuture)7 Test (org.junit.jupiter.api.Test)6 PutObjectResponse (software.amazon.awssdk.services.s3.model.PutObjectResponse)6 UploadFileRequest (software.amazon.awssdk.transfer.s3.UploadFileRequest)5 CompletedDirectoryUpload (software.amazon.awssdk.transfer.s3.CompletedDirectoryUpload)4 DirectoryUpload (software.amazon.awssdk.transfer.s3.DirectoryUpload)4 FileUpload (software.amazon.awssdk.transfer.s3.FileUpload)4 Path (java.nio.file.Path)3 AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)3 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)3 Jimfs (com.google.common.jimfs.Jimfs)2 FileSystem (java.nio.file.FileSystem)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2