Search in sources :

Example 1 with AWSS3RuntimeException

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;
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) AWSS3RuntimeException(py.org.fundacionparaguaya.pspserver.common.exceptions.AWSS3RuntimeException) Regions(com.amazonaws.regions.Regions) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 2 with AWSS3RuntimeException

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);
    }
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) AWSS3RuntimeException(py.org.fundacionparaguaya.pspserver.common.exceptions.AWSS3RuntimeException) Regions(com.amazonaws.regions.Regions)

Aggregations

SdkClientException (com.amazonaws.SdkClientException)2 Regions (com.amazonaws.regions.Regions)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 AWSS3RuntimeException (py.org.fundacionparaguaya.pspserver.common.exceptions.AWSS3RuntimeException)2 DeleteObjectRequest (com.amazonaws.services.s3.model.DeleteObjectRequest)1 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)1