Upload, get and delete files from Amazon S3 with Spring Boot 2 and AWS SDK 1.11.325

I’ve added an Amazon S3 implementation following this article but I found some problems and I added some extra methods that make a more complete class. First of all, the way that the article describes the connection to Amazon is deprecated. I kept the configuration through the application.properties file, but I implemented the AWSCredentialsProvider interface, because it’s the one that we need to use with the current client. The constructor new AmazonS3Client(BasicAWSCredentials credentials) is deprecated now, so we need to create a basic client like this:


Where this is the implementation of AWSCredentialsProvider we did in this same class, at the end of it:

    public AWSCredentials getCredentials() {
        return new AWSCredentials() {
            public String getAWSAccessKeyId() {
                return accessKey;

            public String getAWSSecretKey() {
                return secretKey;

    public void refresh() {
        // Refresh credentials config

Also the method to return objects was created, and I implemented a couple of methods to return byte arrays from a file, and a base 64 version of an image:

public S3Object getFileFromS3Bucket(String fileName) {
        S3Object s3Object = null;
        try {
            s3Object = s3client.getObject(new GetObjectRequest(bucketName, fileName));
            log.debug("Successfully get file ", fileName);
        } catch (Exception e) {
            log.error("There has been an error while getting the file " + fileName, e);

        return s3Object;

    public byte[] getByteArrayFromFileS3Bucket(String fileName) throws IOException {
        log.debug("Getting to byte array from file ", fileName);
        InputStream in = getFileFromS3Bucket(fileName).getObjectContent();
        byte[] byteArray = IOUtils.toByteArray(in);

        return byteArray;

    public String base64Image(String fileName) throws IOException {
        log.debug("Encoding to base64 ", fileName);
        byte[] b = getByteArrayFromFileS3Bucket(fileName);
        return new String(Base64.getEncoder().encode(b));

I recommend you to follow the article I mentioned before and if you find any problem check out this class:

Leave a Reply

Your email address will not be published. Required fields are marked *