티스토리 뷰

의존성 추가

      <!-- AWS -->
      <dependency>
         <groupId>com.amazonaws</groupId>
         <artifactId>aws-java-sdk-s3</artifactId>
         <version>1.11.901</version>
      </dependency>

 

AwsS3 객체

- AwsS3.java

public class AwsS3 {

   // Amazon-s3-sdk
   private AmazonS3 s3Client;
   final private String accessKey = "-"; // 액세스키
   final private String secretkey = "-"; // 스크릿 엑세스 키

   private Regions clientRegion = Regions.AP_NORTHEAST_2; // 한국
   private String bucket = "-"; // 버킷 명

   private AwsS3() {
      createS3Client();
   }

   // singleton 으로 구현
   static private AwsS3 instance = null;

   public static AwsS3 getInstance() {
      if (instance == null) {
         return new AwsS3();
      } else {
         return instance;
      }
   }

   // aws S3 client 생성
   private void createS3Client() {
      AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretkey);
      this.s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials))
            .withRegion(clientRegion).build();
   }

   // upload 메서드 | 단일 파일 업로드
   public void upload(File file, String key) {
      uploadToS3(new PutObjectRequest(this.bucket, key, file));
   }

   // upload 메서드 | MultipartFile을 사용할 경우
   public void upload(File file, String key, String contentType, long contentLength) {
      ObjectMetadata objectMetadata = new ObjectMetadata();
      objectMetadata.setContentType(contentType);
   }

   // PutObjectRequest는 Aws s3 버킷에 업로드할 객체 메타 데이터와 파일 데이터로 이루어져 있다.
   private void uploadToS3(PutObjectRequest putObjectRequest) {
      try {
         this.s3Client.putObject(putObjectRequest);
         System.out.println(String.format("[%s] upload complete", putObjectRequest.getKey()));
      } catch (AmazonServiceException e) {
         e.printStackTrace();
      } catch (SdkClientException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }


   // 복사 메서드
   public void copy(String orgkey, String copyKey) {
      try {
         // copy 객체 생성
         CopyObjectRequest copyObjectRequest = new CopyObjectRequest(this.bucket, orgkey, this.bucket, copyKey);

         // copy
         this.s3Client.copyObject(copyObjectRequest);

         System.out.printf(String.format("Finish copying [%s] to [%s]"), orgkey, copyKey);
      } catch (AmazonServiceException e) {
         e.printStackTrace();
      } catch (SdkClientException e) {
         e.printStackTrace();
      }
   }

   // 삭제 메서드
   public void delete(String key) {
      try {
         // Delete 객체 생성
         DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(this.bucket, key);

         // Delete
         this.s3Client.deleteObject(deleteObjectRequest);

         System.out.printf(String.format("[%s] delete key"), key);
      } catch (AmazonServiceException e) {
         e.printStackTrace();
      } catch (SdkClientException e) {
         e.printStackTrace();
      }
   }

 

테스트 클래스

- main.java

package kr.ac.zerco.aws.main;

import java.io.File;

import kr.ac.zerco.aws.AwsS3;

public class Main {

   public AwsS3 awsS3 = AwsS3.getInstance();

   public static void main(String[] args) {

      Main main = new Main();
      File file = new File("C:\\img\\log.png");

      String key = "img/mainlogo.png";
      //String copyKey = "img/my-img-copy.img";

      //upload 실행하기.
      main.upload(file,key);
   }

   //업로드
   public void upload(File file, String key) {
      awsS3.upload(file, key);
   }

   //복사 메서드
   public void copy(String orgkey, String copyKey) {
      awsS3.copy(orgkey, copyKey);
   }

   //삭제 메서드
   public void delete(String key) {
      awsS3.delete(key);
   }

}

 

댓글