Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- spring jpa
- spring-mvc
- tomcat
- JPA
- Spring
- spring-webmvc
- docker
- IntelliJ
- Data REST
- spring-webmvc #코드읽기
- Spring Data REST
- spring bean
- 톰캣
- 코드 리뷰
- JUnit
- SuperTypeToken
- spring camp
- spring boot tomcat
- Spring Data JPA
- JPA mapping
- ApplicationPidFileWriter
- spring batch 코드
- spring boot
- batch
- REST API
- static inner class
- ORM
- spring pid
- 세미나
- Spring Batch
Archives
- Today
- Total
woniper
[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
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 ListgetBucketList() { 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(); } }
4. S3Util 사용
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(); Listlist = 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")); } }
5. 결과 화면
생성된 Bucket
생성된 폴더와 업로드된 파일
'Java' 카테고리의 다른 글
java8 stream match (0) | 2016.12.03 |
---|---|
SuperTypeToken #2 TypeToken(ModelMapper)과 ParameterizedTypeReference(Spring) 차이 (0) | 2016.11.21 |
SuperTypeToken #1 TypeToken(ModelMapper)과 ParameterizedTypeReference(Spring) 사용법 (0) | 2016.11.12 |
HashMap.put의 return 객체는 무엇일까? (2) | 2016.08.05 |
[java] AES256 암호화 (4) | 2014.07.13 |
Comments