Java SDK
- 인쇄
- PDF
Java SDK
- 인쇄
- PDF
기사 요약
이 요약이 도움이 되었나요?
의견을 보내 주셔서 감사합니다.
Classic/VPC 환경에서 이용 가능합니다.
Java SDK for S3 API
AWS S3에서 제공하는 Java SDK를 이용하여 네이버 클라우드 플랫폼[공공기관용] Object Storage를 사용하는 방법을 설명합니다.
이 문서는 AWS Java SDK 1.11.238 버전을 기준으로 작성되었습니다.
설치
SDK 소스를 직접 다운로드 받거나 Apache Maven을 이용하여 사용하실 수 있습니다.
소스 다운로드
AWS Java SDK GitHub : AWS Java SDK GitHub
Apache Maven으로 이용하기
pom.xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.238</version>
</dependency>
예제
예제에서 사용하는 Access Key, Secret Key는 등록한 API 인증키 정보로 입력해야 합니다.
버킷 생성
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
String bucketName = "sample-bucket";
try {
// create bucket if the bucket name does not exist
if (s3.doesBucketExistV2(bucketName)) {
System.out.format("Bucket %s already exists.\n", bucketName);
} else {
s3.createBucket(bucketName);
System.out.format("Bucket %s has been created.\n", bucketName);
}
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
버킷 목록 조회
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
try {
List<Bucket> buckets = s3.listBuckets();
System.out.println("Bucket List: ");
for (Bucket bucket : buckets) {
System.out.println(" name=" + bucket.getName() + ", creation_date=" + bucket.getCreationDate() + ", owner=" + bucket.getOwner().getId());
}
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
버킷 삭제
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
String bucketName = "sample-bucket";
try {
// delete bucket if the bucket exists
if (s3.doesBucketExistV2(bucketName)) {
// delete all objects
ObjectListing objectListing = s3.listObjects(bucketName);
while (true) {
for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator.hasNext();) {
S3ObjectSummary summary = (S3ObjectSummary)iterator.next();
s3.deleteObject(bucketName, summary.getKey());
}
if (objectListing.isTruncated()) {
objectListing = s3.listNextBatchOfObjects(objectListing);
} else {
break;
}
}
// abort incomplete multipart uploads
MultipartUploadListing multipartUploadListing = s3.listMultipartUploads(new ListMultipartUploadsRequest(bucketName));
while (true) {
for (Iterator<?> iterator = multipartUploadListing.getMultipartUploads().iterator(); iterator.hasNext();) {
MultipartUpload multipartUpload = (MultipartUpload)iterator.next();
s3.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, multipartUpload.getKey(), multipartUpload.getUploadId()));
}
if (multipartUploadListing.isTruncated()) {
ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
listMultipartUploadsRequest.withUploadIdMarker(multipartUploadListing.getNextUploadIdMarker());
multipartUploadListing = s3.listMultipartUploads(listMultipartUploadsRequest);
} else {
break;
}
}
s3.deleteBucket(bucketName);
System.out.format("Bucket %s has been deleted.\n", bucketName);
} else {
System.out.format("Bucket %s does not exist.\n", bucketName);
}
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
오브젝트 업로드
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
String bucketName = "sample-bucket";
// create folder
String folderName = "sample-folder/";
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(0L);
objectMetadata.setContentType("application/x-directory");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName, new ByteArrayInputStream(new byte[0]), objectMetadata);
try {
s3.putObject(putObjectRequest);
System.out.format("Folder %s has been created.\n", folderName);
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
// upload local file
String objectName = "sample-object";
String filePath = "/tmp/sample.txt";
try {
s3.putObject(bucketName, objectName, new File(filePath));
System.out.format("Object %s has been created.\n", objectName);
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
오브젝트 목록 조회
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
String bucketName = "sample-bucket";
// list all in the bucket
try {
ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.withBucketName(bucketName)
.withMaxKeys(300);
ObjectListing objectListing = s3.listObjects(listObjectsRequest);
System.out.println("Object List:");
while (true) {
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" name=" + objectSummary.getKey() + ", size=" + objectSummary.getSize() + ", owner=" + objectSummary.getOwner().getId());
}
if (objectListing.isTruncated()) {
objectListing = s3.listNextBatchOfObjects(objectListing);
} else {
break;
}
}
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
// top level folders and files in the bucket
try {
ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.withBucketName(bucketName)
.withDelimiter("/")
.withMaxKeys(300);
ObjectListing objectListing = s3.listObjects(listObjectsRequest);
System.out.println("Folder List:");
for (String commonPrefixes : objectListing.getCommonPrefixes()) {
System.out.println(" name=" + commonPrefixes);
}
System.out.println("File List:");
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" name=" + objectSummary.getKey() + ", size=" + objectSummary.getSize() + ", owner=" + objectSummary.getOwner().getId());
}
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
오브젝트 다운로드
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
String bucketName = "sample-bucket";
String objectName = "sample-object";
String downloadPath = "/tmp/sample-object";
// download object
try {
S3Object s3Object = s3.getObject(bucketName, objectName);
S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadPath));
byte[] bytesArray = new byte[4096];
int bytesRead = -1;
while ((bytesRead = s3ObjectInputStream.read(bytesArray)) != -1) {
outputStream.write(bytesArray, 0, bytesRead);
}
outputStream.close();
s3ObjectInputStream.close();
System.out.format("Object %s has been downloaded.\n", objectName);
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
오브젝트 삭제
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
String bucketName = "sample-bucket";
String objectName = "sample-object";
// delete object
try {
s3.deleteObject(bucketName, objectName);
System.out.format("Object %s has been deleted.\n", objectName);
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
ACL 설정
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
String bucketName = "sample-bucket";
// set bucket ACL
try {
// get the current ACL
AccessControlList accessControlList = s3.getBucketAcl(bucketName);
// add read permission to anonymous
accessControlList.grantPermission(GroupGrantee.AllUsers, Permission.Read);
s3.setBucketAcl(bucketName, accessControlList);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
String objectName = "sample-object";
String userId = "test-user-02";
// set object ACL
try {
// get the current ACL
AccessControlList accessControlList = s3.getObjectAcl(bucketName, objectName);
// add read permission to user by ID
accessControlList.grantPermission(new CanonicalGrantee(userId), Permission.Read);
s3.setObjectAcl(bucketName, objectName, accessControlList);
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
멀티 파트 업로드
주의
멀티 파트 업로드를 완료하지 않을 경우 잔여 파일이 버킷에 남게 되며, 잔여 파일은 버킷 용량에 포함되어 과금됩니다.
아래 방법을 통해 불완전한 멀티파트 객체를 삭제하여 불필요한 과금이 되지 않도록 주의하시기 바랍니다.
- 취소 또는 완료되지 않은 멀티파트 업로드 정보 조회: ListMultipartUploads API 가이드 참고
- 멀티 파트 삭제: AbortMultipartUpload API 가이드 참고
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
String bucketName = "sample-bucket";
String objectName = "sample-large-object";
File file = new File("/tmp/sample.file");
long contentLength = file.length();
long partSize = 10 * 1024 * 1024;
try {
// initialize and get upload ID
InitiateMultipartUploadResult initiateMultipartUploadResult = s3.initiateMultipartUpload(new InitiateMultipartUploadRequest(bucketName, objectName));
String uploadId = initiateMultipartUploadResult.getUploadId();
// upload parts
List<PartETag> partETagList = new ArrayList<PartETag>();
long fileOffset = 0;
for (int i = 1; fileOffset < contentLength; i++) {
partSize = Math.min(partSize, (contentLength - fileOffset));
UploadPartRequest uploadPartRequest = new UploadPartRequest()
.withBucketName(bucketName)
.withKey(objectName)
.withUploadId(uploadId)
.withPartNumber(i)
.withFile(file)
.withFileOffset(fileOffset)
.withPartSize(partSize);
UploadPartResult uploadPartResult = s3.uploadPart(uploadPartRequest);
partETagList.add(uploadPartResult.getPartETag());
fileOffset += partSize;
}
// abort
// s3.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, objectName, uploadId));
// complete
CompleteMultipartUploadResult completeMultipartUploadResult = s3.completeMultipartUpload(new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, partETagList));
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
고객 제공 키를 사용한 암호화(SSE-C) 요청
SSE-C 기반으로 오브젝트를 암호화 할 경우, 콘솔에서 일부 요청을 사용하실 수 없습니다.
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
// create encryption key
KeyGenerator KEY_GENERATOR = KeyGenerator.getInstance("AES");
KEY_GENERATOR.init(256, new SecureRandom());
final SSECustomerKey SSE_KEY = new SSECustomerKey(KEY_GENERATOR.generateKey());
String bucketName = "sample-bucket";
// upload local file
String objectName = "sample-object";
String filePath = "/tmp/sample.txt";
try {
PutObjectRequest putRequest = new PutObjectRequest(bucketName, objectName, new File(filePath))
.withSSECustomerKey(SSE_KEY);
s3.putObject(putRequest);
System.out.format("Object %s has been created.\n", objectName);
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch (SdkClientException e) {
e.printStackTrace();
}
// download object
String downloadPath = "/tmp/sample-object";
try {
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName)
.withSSECustomerKey(SSE_KEY);
S3Object s3Object = s3.getObject(getObjectRequest);
S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadPath));
byte[] bytesArray = new byte[4096];
int bytesRead = -1;
while ((bytesRead = s3ObjectInputStream.read(bytesArray)) != -1) {
outputStream.write(bytesArray, 0, bytesRead);
}
outputStream.close();
s3ObjectInputStream.close();
System.out.format("Object %s has been downloaded.\n", objectName);
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch (SdkClientException e) {
e.printStackTrace();
} s3ObjectInputStream.close();
System.out.format("Object %s has been downloaded.\n", objectName);
} catch (AmazonS3Exception e) {
e.printStackTrace();
} catch(SdkClientException e) {
e.printStackTrace();
}
CORS(Cross-Origin Resource Sharing) 설정
final String endPoint = "https://kr.object.gov-ncloudstorage.com";
final String regionName = "gov-standard";
final String accessKey = "ACCESS_KEY";
final String secretKey = "SECRET_KEY";
// S3 client
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, regionName))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();
String bucketName = "sample-bucket";
// Create CORS rules.
List<CORSRule.AllowedMethods> methodRule = new ArrayList<CORSRule.AllowedMethods>();
methodRule.add(CORSRule.AllowedMethods.PUT);
methodRule.add(CORSRule.AllowedMethods.GET);
methodRule.add(CORSRule.AllowedMethods.POST);
CORSRule rule = new CORSRule().withId("CORSRule")
.withAllowedMethods(methodRule)
.withAllowedHeaders(Arrays.asList(new String[] { "*" }))
.withAllowedOrigins(Arrays.asList(new String[] { "*" }))
.withMaxAgeSeconds(3000);
List<CORSRule> rules = new ArrayList<CORSRule>();
rules.add(rule);
// Add rules to new CORS configuration.
BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();
configuration.setRules(rules);
// Set the rules to CORS configuration.
s3.setBucketCrossOriginConfiguration(bucketName, configuration);
// Get the rules for CORS configuration.
configuration = s3.getBucketCrossOriginConfiguration(bucketName);
if (configuration == null) {
System.out.println("Configuration is null.");
} else {
System.out.println("Configuration has " + configuration.getRules().size() + " rules\n");
for (CORSRule getRule : configuration.getRules()) {
System.out.println("Rule ID: " + getRule.getId());
System.out.println("MaxAgeSeconds: " + getRule.getMaxAgeSeconds());
System.out.println("AllowedMethod: " + getRule.getAllowedMethods());
System.out.println("AllowedOrigins: " + getRule.getAllowedOrigins());
System.out.println("AllowedHeaders: " + getRule.getAllowedHeaders());
System.out.println("ExposeHeader: " + getRule.getExposedHeaders());
System.out.println();
}
}
이 문서가 도움이 되었습니까?