All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crabshue.commons.aws.s3.AwsS3ClientImpl Maven / Gradle / Ivy

package com.crabshue.commons.aws.s3;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.crabshue.commons.aws.exceptions.AwsErrorType;
import com.crabshue.commons.aws.s3.exceptions.AwsS3ErrorContext;
import com.crabshue.commons.aws.s3.exceptions.AwsS3ErrorType;
import com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.file.FileIOUtils;

/**
 * Implementation for {@link AwsS3Client}.
 *
 */
public class AwsS3ClientImpl implements AwsS3Client {

    private static Logger logger = LoggerFactory.getLogger(AwsS3ClientImpl.class);

    private final AwsS3Bucket awsS3Bucket;

    private final AmazonS3 amazonS3;

    AwsS3ClientImpl(final AmazonS3 amazonS3, final AwsS3Bucket awsS3Bucket) {
        this.amazonS3 = amazonS3;
        this.awsS3Bucket = awsS3Bucket;
    }

    @Override
    public PutObjectResult uploadObject(final String key, final File file) {
        Validate.notNull(key);
        Validate.notNull(file);

        final PutObjectResult ret;
        try {
            ret = amazonS3.putObject(awsS3Bucket.getName(), key, file);
        } catch (AmazonServiceException e) {
            throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e)
                .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.WRITE);
        } catch (SdkClientException e) {
            throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e);
        }
        logger.info("Uploaded file [{}] to AWS S3 bucket [{}] : [{}]", file, awsS3Bucket.getName(), ret);
        return ret;
    }

    @Override
    public PutObjectResult uploadObject(final String key, final String content) {
        Validate.notNull(key);
        Validate.notNull(content);

        final PutObjectResult ret;
        try {
            ret = amazonS3.putObject(awsS3Bucket.getName(), key, content);
        } catch (AmazonServiceException e) {
            throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e)
                .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.WRITE);
        } catch (SdkClientException e) {
            throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e);
        }
        logger.info("Uploaded content to AWS S3 bucket [{}] : [{}]", awsS3Bucket.getName(), ret);
        return ret;
    }

    @Override
    public PutObjectResult uploadObject(final String key, final InputStream inputStream) {
        return uploadObject(key, inputStream, new ObjectMetadata());
    }

    @Override
    public PutObjectResult uploadObject(final String key, final InputStream inputStream, final ObjectMetadata objectMetadata) {
        Validate.notNull(key);
        Validate.notNull(inputStream);

        final PutObjectResult ret;
        try {
            ret = amazonS3.putObject(awsS3Bucket.getName(), key, inputStream, objectMetadata);
        } catch (AmazonServiceException e) {
            throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e)
                .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.WRITE);
        } catch (SdkClientException e) {
            throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e);
        }
        logger.info("Uploaded stream to AWS S3 bucket [{}] : [{}]", awsS3Bucket.getName(), ret);
        return ret;
    }

    @Override
    public void deleteObject(final String key) {
        Validate.notNull(key);
        try {
            amazonS3.deleteObject(awsS3Bucket.getName(), key);

            logger.info("Deleted object [{}] from AWS S3 bucket [{}]", key, awsS3Bucket.getName());

        } catch (AmazonServiceException e) {
            throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e)
                .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.DELETE);

        } catch (SdkClientException e) {
            throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e);
        }
    }

    @Override
    public File downloadObject(final String key, final File outputFile) {
        Validate.notNull(key);

        try {
            final S3Object o = amazonS3.getObject(awsS3Bucket.getName(), key);
            try (S3ObjectInputStream s3is = o.getObjectContent()) {
                FileIOUtils.writeFile(s3is, outputFile);
            }
        } catch (AmazonServiceException | IOException e) {
            throw new SystemException(AwsS3ErrorType.CANNOT_READ_OBJECT, e)
                .addContextValue(AwsS3ErrorContext.KEY, key)
                .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.READ);
        } catch (SdkClientException e) {
            throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e);
        }

        logger.info("Downloaded object [{}] from AWS S3 bucket [{}} to [{}]", key, awsS3Bucket.getName(), outputFile);
        return outputFile;
    }

    @Override
    public Collection listObjects() {
        try {
            ListObjectsV2Result result = amazonS3.listObjectsV2(awsS3Bucket.getName());
            final List ret = result.getObjectSummaries();

            logger.info("List [{}] objects from AWS S3 bucket [{}]", ret.size(), awsS3Bucket.getName());
            return ret;
        } catch (AmazonServiceException e) {
            throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e)
                .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.LIST);
        } catch (SdkClientException e) {
            throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e);
        }
    }

    @Override
    public Collection listObjects(final String prefix) {
        Validate.notNull(prefix);

        try {
            ListObjectsV2Result result = amazonS3.listObjectsV2(awsS3Bucket.getName(), prefix);
            final List ret = result.getObjectSummaries();

            logger.info("List [{}] objects with prefix [{}] from AWS S3 bucket [{}]", ret.size(), prefix, awsS3Bucket.getName());
            return ret;
        } catch (AmazonServiceException e) {
            throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e)
                .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.LIST)
                .addContextValue(AwsS3ErrorContext.PREFIX, prefix);
        } catch (SdkClientException e) {
            throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e);
        }
    }

    @Override
    public void deleteObjects(final Collection keys) {

        if (keys.isEmpty()) {
            logger.info("AWS S3 bucket [{}] is empty. Nothing to delete.", awsS3Bucket.getName());
            return;
        }

        final List keyVersions =
            keys.stream()
                .map(DeleteObjectsRequest.KeyVersion::new)
                .collect(Collectors.toList());

        try {
            DeleteObjectsRequest dor = new DeleteObjectsRequest(awsS3Bucket.getName())
                .withKeys(keyVersions);
            amazonS3.deleteObjects(dor);
            logger.info("Deleted [{}] objects from AWS S3 bucket [{}]", keys.size(), awsS3Bucket.getName());
        } catch (AmazonServiceException e) {
            throw new SystemException(AwsS3ErrorType.AWS_S3_ERROR, e)
                .addContextValue(AwsS3ErrorContext.OPERATION, AwsS3ErrorContext.DELETE);
        } catch (SdkClientException e) {
            throw new SystemException(AwsErrorType.AWS_SDK_CONFIGURATION_ERROR, e);
        }
    }

    @Override
    public void deleteAll() {
        final List keys = this.listObjects().stream()
            .map(S3ObjectSummary::getKey)
            .collect(Collectors.toList());

        this.deleteObjects(keys);

        logger.info("Deleted all objects in AWS S3 bucket [{}]", awsS3Bucket.getName());
    }

    @Override
    public void copyFile(final String sourceKey, final String targetKey) {
        amazonS3.copyObject(this.awsS3Bucket.getName(), sourceKey, this.awsS3Bucket.getName(), targetKey);

        logger.info("Copied [{}] to [{}]", sourceKey, targetKey);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy