0

S3TransferManager を使用してファイルを s3 にアップロードしようとしています。しかし、私のユニットテストは以下のエラーのために失敗します.

java.util.concurrent.CompletionException: software.amazon.awssdk.services.s3.model.S3Exception: リクエストからの無効なレスポンスステータス

ここに私のコードがあります、

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();
    }


}

そして、これが上記のコードの単体テストです。

    @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());
}

上記のエラーで単体テストが失敗する理由を教えてください。

また、「s3ClientConfiguration」をモックする他の方法はありますか? aws Java SDK v2で

4

0 に答える 0