I am testing my code in my test class as follows:
`
@BeforeMethod(groups="dao")
public void beforeMethod() {
api = new S3Mock.Builder().withPort(8001).withInMemoryBackend().build();
api.start();
s3Dao = new S3DaoImpl(AppConstants.S3_ENDPOINT_LOCAL);
base64ImageDto = new Base64ImageDto("data:image/png;base64,324987sdfjkasdf", "test_dao_file.png");
preSignedFileName = "test-nhs-logo-3.png";
}
@AfterMethod(groups="dao")
public void afterMethod(){
api.shutdown();
}
/*
* Test the upload image functionality of the service
*/
@Test(groups="dao")
public void testS3DaoUploadBase64Image() {
InitActions.testCaseDescription.set("Testing s3Dao upload base64image function.");
String key = s3Dao.uploadBase64Image("TESTTEST", base64ImageDto);
Assert.assertNotNull(key);
}`
and the Dao implementation is here:
`
public String uploadBase64Image(String nhsId, Base64ImageDto base64ImageDto) {
InputStream stream = new ByteArrayInputStream(base64ImageDto.getImageBytes());
AWSCredentials credentials = new BasicAWSCredentials(userId, secretId);
try {
LOGGER.info("Uploading file " + base64ImageDto.getFileName() + " to UKCloud S3");
AmazonS3 s3Client;
if(AppConstants.S3_ENDPOINT_LOCAL.equalsIgnoreCase(endPoint)) {
AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration("http://localhost:8001", "us-west-2");
s3Client = AmazonS3ClientBuilder
.standard()
.withPathStyleAccessEnabled(true)
.withEndpointConfiguration(endpoint)
.withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()))
.build();
} else {
s3Client = AmazonS3ClientBuilder.standard()
.withCredentials( new AWSStaticCredentialsProvider(credentials))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, signingRegion))
.withPathStyleAccessEnabled(true)
.build();
}
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(base64ImageDto.getImageBytes().length);
metadata.setContentType("image/"+base64ImageDto.getFileType());
// Create Object key for upload
Instant instant = Instant.now().truncatedTo( ChronoUnit.SECONDS );
String dateTime = instant.toString(); // Generate a `String` object in standard ISO 8601 format.
// Key format = <NHS#>_<TEMPLATE#>_<DATETIME>.<FILE TYPE>
String key = nhsId + "_" + base64ImageDto.getFileName().substring(0, base64ImageDto.getFileName().indexOf("."))+ "_" + dateTime + "." + base64ImageDto.getFileType();
PutObjectRequest request = new PutObjectRequest(bucketName, key, stream, metadata);
if(AppConstants.S3_ENDPOINT_LOCAL.equalsIgnoreCase(endPoint)) {
s3Client.createBucket("zeus-webapp-drop-test");
}
s3Client.putObject(request);
return key;
// TODO: return the URL location for & store in documentReference object
} catch(AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
LOGGER.info("Error uploading file " + base64ImageDto.getFileName() + " to S3");
LOGGER.error(e);
return "error";
}
catch(SdkClientException e) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
LOGGER.info("Error uploading file " + base64ImageDto.getFileName() + " to S3");
LOGGER.error(e);
return "error";
}
}`
The method hangs when it gets to s3Client.createBucket("zeus-webapp-drop-test") and I am not sure how to troubleshoot this as it is a call to the client itself... Has this happened to anyone?
I am testing my code in my test class as follows: ` @BeforeMethod(groups="dao") public void beforeMethod() { api = new S3Mock.Builder().withPort(8001).withInMemoryBackend().build(); api.start(); s3Dao = new S3DaoImpl(AppConstants.S3_ENDPOINT_LOCAL); base64ImageDto = new Base64ImageDto("data:image/png;base64,324987sdfjkasdf", "test_dao_file.png"); preSignedFileName = "test-nhs-logo-3.png"; }
and the Dao implementation is here: ` public String uploadBase64Image(String nhsId, Base64ImageDto base64ImageDto) { InputStream stream = new ByteArrayInputStream(base64ImageDto.getImageBytes()); AWSCredentials credentials = new BasicAWSCredentials(userId, secretId);
The method hangs when it gets to s3Client.createBucket("zeus-webapp-drop-test") and I am not sure how to troubleshoot this as it is a call to the client itself... Has this happened to anyone?