use of py.org.fundacionparaguaya.pspserver.common.exceptions.AWSS3RuntimeException in project FP-PSP-SERVER by FundacionParaguaya.
the class ImageUploadServiceImpl method uploadImage.
@Override
public String uploadImage(ImageDTO imageDTO) {
if (imageDTO == null) {
return null;
}
String url;
try {
String strRegion = applicationProperties.getAws().getStrRegion();
Regions region = Regions.valueOf(strRegion);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(region).build();
String bucketName = applicationProperties.getAws().getBucketName();
String imageDirectory = imageDTO.getImageDirectory();
String imageName = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
String fileName = imageName + "." + imageDTO.getFormat();
String keyName = imageDirectory + fileName;
s3Client.putObject(new PutObjectRequest(bucketName, keyName, imageDTO.getFile()).withCannedAcl(CannedAccessControlList.PublicRead));
url = "https://s3." + s3Client.getRegionName() + ".amazonaws.com/" + bucketName + "/" + keyName;
} catch (SdkClientException sdkClientExc) {
LOG.error(sdkClientExc.getMessage(), sdkClientExc);
throw new AWSS3RuntimeException(sdkClientExc);
}
return url;
}
use of py.org.fundacionparaguaya.pspserver.common.exceptions.AWSS3RuntimeException in project FP-PSP-SERVER by FundacionParaguaya.
the class ImageUploadServiceImpl method deleteImage.
@Override
public void deleteImage(String logoUrl, String imageDirectory) {
if (logoUrl == null) {
return;
}
try {
String strRegion = applicationProperties.getAws().getStrRegion();
Regions region = Regions.valueOf(strRegion);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(region).build();
String bucketName = applicationProperties.getAws().getBucketName();
String fileName = logoUrl.substring(logoUrl.lastIndexOf('/') + 1);
String keyName = imageDirectory + fileName;
s3Client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
} catch (SdkClientException sdkClientExc) {
LOG.error(sdkClientExc.getMessage(), sdkClientExc);
throw new AWSS3RuntimeException(sdkClientExc);
}
}
Aggregations