Java용 AWS SDK
  • PDF

Java용 AWS SDK

  • PDF

Classic/VPC 환경에서 이용 가능합니다.

Java용 SDK for S3 API

AWS S3에서 제공하는 Java용 SDK를 이용해 네이버 클라우드 플랫폼의 Object Storage를 사용하는 예제입니다. AWS Java SDK 1.11.238 버전을 기준으로 작성되었습니다.

SDK 설치

SDK 소스를 직접 다운로드하거나 Apache Maven을 이용해 설치할 수 있습니다.

  • 직접 다운로드하기

AWS Java SDK GitHub: https://github.com/aws/aws-sdk-java/tree/master/aws-java-sdk-s3

  • Apache Maven 이용하기

pom.xml

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.11.238</version>
</dependency>

예제

참고

예제에서 사용하는 accessKey, secretKey 값에는 등록한 API 인증키 정보를 입력해야 합니다.

버킷 생성

final String endPoint = "https://kr.object.ncloudstorage.com";
final String regionName = "kr-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.ncloudstorage.com";
final String regionName = "kr-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.ncloudstorage.com";
final String regionName = "kr-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.ncloudstorage.com";
final String regionName = "kr-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.ncloudstorage.com";
final String regionName = "kr-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.ncloudstorage.com";
final String regionName = "kr-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.txt";
String downloadFilePath = "/tmp/sample-object.txt";

// download object
try {
    S3Object s3Object = s3.getObject(bucketName, objectName);
    S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();

    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFilePath));
    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.ncloudstorage.com";
final String regionName = "kr-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.ncloudstorage.com";
final String regionName = "kr-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();
}

멀티 파트 업로드

final String endPoint = "https://kr.object.ncloudstorage.com";
final String regionName = "kr-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.ncloudstorage.com";
final String regionName = "kr-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();
}

CORS(Cross-Origin Resource Sharing) 설정

final String endPoint = "https://kr.object.ncloudstorage.com";
final String regionName = "kr-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();
    }
}

이 글이 도움이 되었나요?