Menu
AWS SDK for Java
Developer Guide

Performing Operations on Amazon S3 Objects

An Amazon S3 object represents a file or collection of data. Every object must reside within a bucket.

Note

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

Upload an Object

Use the AmazonS3 client's putObject method, supplying a bucket name, key name, and file to upload. The bucket must exist, or an error will result.

Imports

Copy
import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.AmazonServiceException;

Code

Copy
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { s3.putObject(bucket_name, key_name, file_path); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

See the complete example.

List Objects

To get a list of objects within a bucket, use the AmazonS3 client's listObjects method, supplying the name of a bucket.

The listObjects method returns an ObjectListing object that provides information about the objects in the bucket. To list the object names (keys), use the getObjectSummaries method to get a List of S3ObjectSummary objects, each of which represents a single object in the bucket. Then call its getKey method to retrieve the object's name.

Imports

Copy
import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.s3.model.S3ObjectSummary; import java.util.List;

Code

Copy
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); ObjectListing ol = s3.listObjects(bucket_name); List<S3ObjectSummary> objects = ol.getObjectSummaries(); for (S3ObjectSummary os: objects) { System.out.println("* " + os.getKey()); }

See the complete example.

Download an Object

Use the AmazonS3 client's getObject method, passing it the name of a bucket and object to download. If successful, the method returns an S3Object. The specified bucket and object key must exist, or an error will result.

You can get the object's contents by calling getObjectContent on the S3Object. This returns an S3ObjectInputStream that behaves as a standard Java InputStream object.

The following example downloads an object from S3 and saves its contents to a file (using the same name as the object's key).

Imports

Copy
import com.amazonaws.AmazonServiceException; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3ObjectInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;

Code

Copy
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { S3Object o = s3.getObject(bucket_name, key_name); S3ObjectInputStream s3is = o.getObjectContent(); FileOutputStream fos = new FileOutputStream(new File(key_name)); byte[] read_buf = new byte[1024]; int read_len = 0; while ((read_len = s3is.read(read_buf)) > 0) { fos.write(read_buf, 0, read_len); } s3is.close(); fos.close(); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } catch (FileNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); }

See the complete example.

Copy, Move, or Rename Objects

You can copy an object from one bucket to another by using the AmazonS3 client's copyObject method. It takes the name of the bucket to copy from, the object to copy, and the destination bucket and name.

Imports

Copy
import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.AmazonServiceException;

Code

Copy
final AmazonS3 s3 = new AmazonS3Client(); try { s3.copyObject(from_bucket, object_key, to_bucket, object_key); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

See the complete example.

Note

You can use copyObject with deleteObject to move or rename an object, by first copying the object to a new name (you can use the same bucket as both the source and destination) and then deleting the object from its old location.

Delete an Object

Use the AmazonS3 client's deleteObject method, passing it the name of a bucket and object to delete. The specified bucket and object key must exist, or an error will result.

Imports

Copy
import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.AmazonServiceException;

Code

Copy
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { s3.deleteObject(bucket_name, object_key); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

See the complete example.

Delete Multiple Objects at Once

Using the AmazonS3 client's deleteObjects method, you can delete multiple objects from the same bucket by passing their names to the DeleteObjectRequest withKeys method.

Imports

Copy
import com.amazonaws.AmazonServiceException; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.DeleteObjectsRequest;

Code

Copy
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); try { DeleteObjectsRequest dor = new DeleteObjectsRequest(bucket_name) .withKeys(object_keys); s3.deleteObjects(dor); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); }

See the complete example.