[AWS : Amazon Web Service] Java S3 사용
https://github.com/Woniper/JavaExample/tree/master/AWS-S3-JAVA
1. S3 설정
- S3 사용방법
- AWS SDK 다운로드 : http://aws.amazon.com/ko/sdkforjava/
- 라이브러리 추가(AWS SDK는 용량 초과로 위 url에서 다운받아야함.)
2. S3 Access Key 생성
오른쪽 상단에 Security Credentials 선택
Access Keys 선택 > Create New Access Key
Show Access Key 선택하면 Access Key ID와 Secret Access Key가 생성된걸 볼 수 있다.
3. S3Util 구현
S3 Endpoint URL : http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | package com.woniper.aws; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.List; import com.amazonaws.ClientConfiguration; import com.amazonaws.Protocol; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.Bucket; import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; import com.amazonaws.services.s3.model.ObjectMetadata; public class S3Util { private String accessKey = "put your access Key" ; private String secretKey = "put your secret Key" ; private AmazonS3 conn; public S3Util() { AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.HTTP); this .conn = new AmazonS3Client(credentials, clientConfig); conn.setEndpoint( "s3-ap-northeast-1.amazonaws.com" ); } // Bucket List public List<bucket> getBucketList() { return conn.listBuckets(); } // Bucket 생성 public Bucket createBucket(String bucketName) { return conn.createBucket(bucketName); } // 폴더 생성 (폴더는 파일명 뒤에 "/"를 붙여야한다.) public void createFolder(String bucketName, String folderName) { conn.putObject(bucketName, folderName + "/" , new ByteArrayInputStream( new byte [ 0 ]), new ObjectMetadata()); } // 파일 업로드 public void fileUpload(String bucketName, File file) throws FileNotFoundException { conn.putObject(bucketName, file.getName(), new FileInputStream(file), new ObjectMetadata()); } // 파일 삭제 public void fileDelete(String bucketName, String fileName) { conn.deleteObject(bucketName, fileName); } // 파일 URL public String getFileURL(String bucketName, String fileName) { return conn.generatePresignedUrl( new GeneratePresignedUrlRequest(bucketName, fileName)).toString(); } } </bucket> |
4. S3Util 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.woniper.aws; import java.io.File; import java.io.FileNotFoundException; import java.util.List; import com.amazonaws.services.s3.model.Bucket; public class MainAWS { public static void main(String[] args) throws FileNotFoundException { S3Util s3 = new S3Util(); List<bucket> list = s3.getBucketList(); // 첫번째 Bucket String bucketName = list.get( 0 ).getName().toString(); System.out.println( "Bucket Name : " + bucketName); // Bucket 생성(대문자는 포함되면 안됨.) for ( int i = 0 ; i < 3 ; i++) { s3.createBucket( "wonier-test-bucket" + i); } // 폴더 생성 for ( int i = 0 ; i < 3 ; i++) { s3.createFolder(bucketName, "woniper-test-folder" + i); } // 파일 업로드 String fileName = "/Users/woniper/Downloads/aws-java-sdk-1.8.4.zip" ; s3.fileUpload(bucketName, new File(fileName)); System.out.println(s3.getFileURL(bucketName, "aws-java-sdk-1.8.4.zip" )); } } </bucket> |
5. 결과 화면
생성된 Bucket
생성된 폴더와 업로드된 파일
출처 : https://blog.woniper.net/218?category=506090
'JAVA > Java' 카테고리의 다른 글
java8 stream match (0) | 2020.09.07 |
---|---|
SuperTypeToken #2 TypeToken(ModelMapper)과 ParameterizedTypeReference(Spring) 차이 (0) | 2020.09.07 |
SuperTypeToken #1 TypeToken(ModelMapper)과 ParameterizedTypeReference(Spring) 사용법 (0) | 2020.09.07 |
HashMap.put의 return 객체는 무엇일까? (0) | 2020.09.07 |
[java] AES256 암호화 (0) | 2020.09.04 |
SuppressWarnings-이건-뭐지 (0) | 2020.07.02 |
[Java] 객체를 제거하는 방법 (0) | 2020.06.10 |
[Java] Wrapper 클래스 (0) | 2020.06.10 |