AWS SDK for Java
Developer Guide

Performing Operations on an Amazon S3 Object

Important

This is a preview release and is not recommended for production environments.

An Amazon S3 object represents a file or collection of data. Every object must be contained in a bucket.

Note

Best Practice

We recommend that you enable the AbortIncompleteMultipartUpload lifecycle rule on your Amazon S3 buckets.

This rule directs Amazon S3 to abort multipart uploads that don't complete within a specified number of days after being initiated. When the set time limit is exceeded, Amazon S3 aborts the upload and then deletes the incomplete upload data.

For more information, see Lifecycle Configuration for a Bucket with Versioning in the Amazon S3 User Guide.

Note

These code snippets assume that you understand the material in Using the AWS SDK for Java 2.0 Developer Preview, and have configured default AWS credentials using the information in Set Up AWS Credentials and Region for Development.

Upload an Object

Build a PutObjectRequest and supply a bucket name and key name. Then use the S3Client's putObject method with a RequestBody that contains the object content and the PutObjectRequest object. The bucket must exist, or the service will return an error.

Imports

import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.ListObjectsV2Response; import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable;

Code

Region region = Region.US_WEST_2; s3 = S3Client.builder().region(region).build(); String bucket = "bucket" + System.currentTimeMillis(); String key = "key"; // Put Object s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key) .build(), RequestBody.fromByteBuffer(getRandomByteBuffer(10_000)));

See the complete example on GitHub.

Upload Objects in Multiple Parts

Use the S3Client's createMultipartUpload method to get an upload ID. Then use the uploadPart method to upload each part. Finally, use the S3Client's completeMultipartUpload method to tell Amazon S3 to merge all the uploaded parts and finish the upload operation.

Imports

import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest; import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload; import software.amazon.awssdk.services.s3.model.CompletedPart; import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest; import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse; import software.amazon.awssdk.services.s3.model.S3Object;

Code

CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder() .bucket(bucketName).key(key) .build(); CreateMultipartUploadResponse response = s3.createMultipartUpload(createMultipartUploadRequest); String uploadId = response.uploadId(); System.out.println(uploadId); // Upload all the different parts of the object UploadPartRequest uploadPartRequest1 = UploadPartRequest.builder().bucket(bucketName).key(key) .uploadId(uploadId) .partNumber(1).build(); String etag1 = s3.uploadPart(uploadPartRequest1, RequestBody.fromByteBuffer(getRandomByteBuffer(5 * MB))).eTag(); CompletedPart part1 = CompletedPart.builder().partNumber(1).eTag(etag1).build(); UploadPartRequest uploadPartRequest2 = UploadPartRequest.builder().bucket(bucketName).key(key) .uploadId(uploadId) .partNumber(2).build(); String etag2 = s3.uploadPart(uploadPartRequest2, RequestBody.fromByteBuffer(getRandomByteBuffer(3 * MB))).eTag(); CompletedPart part2 = CompletedPart.builder().partNumber(2).eTag(etag2).build(); // Finally call completeMultipartUpload operation to tell S3 to merge all uploaded // parts and finish the multipart operation. CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder().parts(part1, part2).build(); CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder().bucket(bucketName).key(key).uploadId(uploadId) .multipartUpload(completedMultipartUpload).build(); s3.completeMultipartUpload(completeMultipartUploadRequest);

See the complete example on GitHub.

Download an Object

Build a GetObjectRequest and supply a bucket name and key name. Use the S3Client's getObject method, passing it the GetObjectRequest object and a ResponseTransformer object. The ResponseTransformer creates a response handler that writes the response content to the specified file or stream.

The following example specifies a file name to write the object content to.

Imports

import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.core.sync.RequestBody;

Code

// Get Object s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(), ResponseTransformer.toFile(Paths.get("multiPartKey")));

See the complete example on GitHub.

Delete an Object

Build a DeleteObjectRequest and supply a bucket name and key name. Use the S3Client's deleteObject method, and pass it the name of a bucket and object to delete. The specified bucket and object key must exist, or the service will return an error.

Imports

import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;

Code

// Delete Object DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucket).key(key).build(); s3.deleteObject(deleteObjectRequest);

See the complete example on GitHub.