앱)AWS S3 Util java spring 예제 샘플

Posted by HULIA(휴리아)
2018. 6. 8. 11:19 백엔드개발/자바스프링
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.0</version>
</dependency>


@Configuration
public class AWSS3Util{

        @Value("${s3.accessKey}")
        private String accessKey;
   
        @Value("${s3.secretKey}")
        private String secretKey;

        @Value("${s3.region}")
        private String region;


        @Bean
         public AmazonS3 amazonS3Client(){
                String tmpAccessKey = "";
                String tmpSecretKey="";

                 if("Local".equals(System.getProperty("server.type"))){
                       tmpAccessKey = accessKey;
                       tmpSecretKey = secretKey;
                 }else{
                      tmpAccessKey = AESCipherUtil.decrypt(accessKey);
                     tmpSecretKey = AESCipherUtil.decrypt(secretKey);
                  }
         
                  AWSCredentials credentials = new BasicAWSCredentials(tmpAccessKey, tmpSecretKey);

                AmazonS3 s3Client = null;
 
                if("Local".equals(System.getProperty("server.type"))){
                     ClientConfiguration clientCfg = new ClientConfiguration();
                     clientCfg.setProtocol(Protocol.HTTP);
                      clientCfg.setProxyHost("");
                     clientCfg.setProxyPort();
                     s3Client = new AmazonS3Client(credentials, clientCfg);

                 }else{
                        s3Client = new AmazonS3Client(credentials);
                  }

                  Region regionCode = null;
      
                  switch (region) {
                        case="EU_WEST_1":
                                   regionCode = Region.getRegion(Regions.EU_WEST_1);
                                   break;
                       case="AP_NORTHEAST_1":
                         regionCode = Region.getRegion(Regions.AP_NORTHEST_1);
                        break;
                  }

                  s3Client.setRegion(regionCode);

               return s3Client;

       }

}