woniper

[AWS : Amazon Web Service] Java S3 사용 본문

Java

[AWS : Amazon Web Service] Java S3 사용

Woniper 2014.07.13 13:05

https://github.com/Woniper/JavaExample/tree/master/AWS-S3-JAVA



1. S3 설정

S3 사용방법

- AWS SDK 다운로드 : http://aws.amazon.com/ko/sdkforjava/

- 라이브러리 추가(AWS SDK는 용량 초과로 위 url에서 다운받아야함.)


commons-logging-1.1.3.jar


httpclient-4.2.3.jar


httpcore-4.2.jar


jackson-annotations-2.4.1.jar


jackson-core-2.4.1.jar


jackson-databind-2.4.1.1.jar


joda-time-2.2.jar



2. S3 Access Key 생성

오른쪽 상단에 Security Credentials 선택


Access Keys 선택 > Create New Access Key


Show Access Key 선택하면 Access Key IDSecret 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 List 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();
	}
	
}


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();
		
		List 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"));
		
	}

}


5. 결과 화면


생성된 Bucket


생성된 폴더와 업로드된 파일



8 Comments
댓글쓰기 폼