am trying to use S3TransferManager to upload file to s3. but my unit test fails due to the below error,

java.util.concurrent.CompletionException: software.amazon.awssdk.services.s3.model.S3Exception: Invalid response status from request

here's my code,

public class AwsTransferService {

private final S3TransferManager s3TransferManager;
private final AwsS3Config s3Config;

public AwsTransferService(AwsS3Config s3Config, AwsConfig awsConfig) {
    this.s3Config = s3Config;

    AwsBasicCredentials awsCredentials = create(awsConfig.getAccessKey(), awsConfig.getSecretKey());
    this.s3TransferManager = S3TransferManager.builder()
            .s3ClientConfiguration(builder -> builder.credentialsProvider(create(awsCredentials))
                    .region(s3Config.getRegion())
                    .minimumPartSizeInBytes(10 * MB)
                    .targetThroughputInGbps(20.0))
            .build();

}

public AwsTransferService(S3TransferManager s3TransferManager, AwsS3Config s3Config) {
    this.s3TransferManager = s3TransferManager;
    this.s3Config = s3Config;
}


public void transferObject(@NonNull String bucketName, @NonNull String transferKey, @NonNull File file) {
    validateS3Key(transferKey);
    validatePath(file.toPath());
    log.info("Transfering s3 object from :{} to :{}", file.getPath(), transferKey);
    try {
        Upload upload =
                s3TransferManager.upload(b -> b.putObjectRequest(r -> r.bucket(bucketName).key(transferKey))
                        .source(file.toPath()));
        CompletedUpload completedUpload = upload.completionFuture().join();

        log.info("PutObjectResponse: " + completedUpload.response());
    } catch (Exception e) {
        e.printStackTrace();
    }


}

and here is my unit test for the above code,

    @RegisterExtension
public static final S3MockExtension S3_MOCK = builder()
        .silent()
        .withSecureConnection(false)
        .build();

private S3ClientConfiguration s3ClientConfiguration;

private AwsTransferService service;

private AwsS3Service awsS3Service;

private S3TransferManager s3TransferManager;

private static S3Client s3Client;

@BeforeAll
public static void beforeAll() {
    s3Client = S3_MOCK.createS3ClientV2();
}

@BeforeEach
public void beforeEach() throws IOException {

   s3ClientConfiguration =mock(S3ClientConfiguration.class);

   s3TransferManager = S3TransferManager.builder().s3ClientConfiguration(s3ClientConfiguration).build();

    AwsS3Config s3Config = AwsS3Config.builder()
            .region(Region.AP_SOUTHEAST_2)
            .s3BucketName(S3Factory.VALID_S3_BUCKET)
            .build();

    awsS3Service = new AwsS3Service(s3Config, s3Client);

    awsS3Service.createBucket(VALID_S3_BUCKET);

    service = new AwsTransferService(s3TransferManager, s3Config);


}

@Test
public void transferObject_singleFile_ShouldUploadFiletoS3() throws IOException {

    String transferKey = TRANSFER_KEY_UPLOAD;
    String fileName = FILE_PATH + TRANSFER_FILE_NAME;
    writeFile(fileName);
    File transferFile = new File(fileName);
    service.transferObject(VALID_S3_BUCKET, transferKey + TRANSFER_FILE_NAME, transferFile);
    int expectedObjectsSize = 1;
    Log.initLoggingToFile(Log.LogLevel.Error, "log.txt");
    List<S3Object> matchedObjects = awsS3Service.listObjectsWithPrefix(transferKey + TRANSFER_FILE_NAME);
    assertEquals(expectedObjectsSize, matchedObjects.size());
    assertEquals(transferKey + TRANSFER_FILE_NAME, matchedObjects.get(0).key());
}

please let me know why the unit test fails with the above mentioned error?

also please let me know is there any other way to mock "s3ClientConfiguration"? in aws java sdk v2

0

There are 0 answers