junit 创建GetRequestObject示例AWS S3 for Mock

uurv41yg  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(134)

我已经写了这个函数来从S3中获取对象,现在我想写单元测试用例。我无法模拟“s3Client.getObjectAsynchronous(objectRequest)”,因此我可以获得“ResponseAsynchronous objectRequest”的模拟响应并继续。

GetObjectRequest objectRequest = GetObjectRequest
                .builder()
                .key(keyName)
                .bucket(bucketName)
                .build();

        ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
        LOGGER.info(objectBytes.toString());
        InputStream inputStream = objectBytes.asInputStream();
        String result = new BufferedReader(new InputStreamReader(inputStream))
                .lines().collect(Collectors.joining("\n"));
        LOGGER.info(result);
6za6bjd0

6za6bjd01#

GetObjectResponse getObjectResponse = GetObjectResponse.builder()
                .acceptRanges("bytes")
                .lastModified(Instant.now())
                .contentLength(38l)
                .eTag("fab88ab08ad0dsadsadsad30c7320bb8")
                .contentType("text/plain")
                .serverSideEncryption("AES256")
                .metadata(new HashMap<>())
                .build();
        byte[] responseBytes = "0x546869732069732066726fasdbnjasnfjfnsafa4b726973686e61".getBytes(StandardCharsets.UTF_8);
        ResponseBytes<GetObjectResponse> getObjectResponseResponseBytes = ResponseBytes.fromByteArray(getObjectResponse, responseBytes);

相关问题