AWS SDK for Java

Prev Next

Available in Classic and VPC

SDK for S3 API for Java

This is an example using Object Storage of NAVER Cloud Platform using SDK for Java provided by AWS S3. The example is based on AWS Java SDK 1.11.238 version.

Install SDK

You can manually download the SDK source or install it using Apache Maven.

  • Downloading manually

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

  • Using Apache Maven

pom.xml

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

Example

Note

You should enter the registered API authentication key information for the accessKey and secretKey values used in the example.

Create bucket

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

View bucket lists

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

Delete bucket

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

Upload files

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

View file lists

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

Download files

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

Delete files

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

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

Upload multi-parts

Caution

If upload of multi-parts is not completed, residual files remain in the bucket. The residual files are included in the bucket capacity and charged. Please be cautioned that the following method to prevent unnecessary charges by deleting incomplete multi-part objects.

  1. Receive notifications for incomplete multi-part upload objects through Cloud Advisor
    1. For more information on Cloud Advisor, see the Cloud Advisor Guide.
  2. Check and delete incomplete multi-part objects
    1. View information on canceled or uncompleted multi-part upload: ListMultipartUploads API Guide
    2. Delete multi-part: AbortMultipartUpload API Guide
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();
}

Request encryption (SSE-C) using keys provided by customers

Note

Some requests are not available in the console if the object is encrypted based on 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();
}

Set cross-origin resource sharing (CORS)

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