Java向け AWS SDK

Prev Next

Classic/VPC環境で利用できます。

Java向け SDK for S3 API

AWS S3で提供する Java向け SDKを利用して NAVERクラウドプラットフォームの 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();
}

マルチパートのアップロード

注意

マルチパートのアップロードを完了しない場合、残りのファイルがバケットに残り、バケット容量に含まれて課金されます。以下の通り不完全なマルチパートのオブジェクトを削除し、不要に課金されないようにご注意ください。

  1. Cloud Advisorから不完全なマルチパートアップロードのオブジェクトについて通知を受信
    1. Cloud Advisorに関する詳細は、Cloud Advisorご利用ガイドをご確認ください。
  2. 不完全なマルチパートのオブジェクトを確認および削除
    1. キャンセルされたか、完了していないマルチパートアップロード情報を照会する: ListMultipartUploads APIガイド
    2. マルチパート削除: AbortMultipartUpload 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";
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();
    }
}