S3Utils.java

  1. /*******************************************************************************
  2.  * Copyright (C) 2020 Ram Sadasiv
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  ******************************************************************************/
  17. package io.outofprintmagazine.corpus.storage.s3;

  18. import java.io.IOException;
  19. import java.util.HashMap;
  20. import java.util.Map;

  21. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  22. import com.amazonaws.services.s3.AmazonS3;
  23. import com.amazonaws.services.s3.AmazonS3ClientBuilder;

  24. import io.outofprintmagazine.util.IParameterStore;

  25. public class S3Utils {

  26.     private IParameterStore parameterStore;
  27.    
  28.     private S3Utils(IParameterStore parameterStore) {
  29.         super();
  30.         this.parameterStore = parameterStore;
  31.     }
  32.    
  33.     private static Map<IParameterStore, S3Utils> instances = new HashMap<IParameterStore, S3Utils>();
  34.    
  35.    
  36.     public static S3Utils getInstance(IParameterStore parameterStore) throws IOException {
  37.         if (instances.get(parameterStore) == null) {
  38.             S3Utils instance = new S3Utils(parameterStore);
  39.             instances.put(parameterStore, instance);
  40.         }
  41.         return instances.get(parameterStore);
  42.     }
  43.    
  44.     public AmazonS3 getS3Client() throws IOException, ClassNotFoundException {
  45.         return AmazonS3ClientBuilder.standard()
  46.                   .withCredentials(new AWSStaticCredentialsProvider(AwsUtils.getInstance(parameterStore).getBasicCredentials()))
  47.                   .withRegion(AwsUtils.getInstance(parameterStore).getRegion())
  48.                   .build();
  49.     }

  50. }