Java S3 Examples

Setup

The following examples may require some or all of the following java classes to be imported:

  1. import java.io.ByteArrayInputStream;
  2. import java.io.File;
  3. import java.util.List;
  4. import com.amazonaws.auth.AWSCredentials;
  5. import com.amazonaws.auth.BasicAWSCredentials;
  6. import com.amazonaws.util.StringUtils;
  7. import com.amazonaws.services.s3.AmazonS3;
  8. import com.amazonaws.services.s3.AmazonS3Client;
  9. import com.amazonaws.services.s3.model.Bucket;
  10. import com.amazonaws.services.s3.model.CannedAccessControlList;
  11. import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
  12. import com.amazonaws.services.s3.model.GetObjectRequest;
  13. import com.amazonaws.services.s3.model.ObjectListing;
  14. import com.amazonaws.services.s3.model.ObjectMetadata;
  15. import com.amazonaws.services.s3.model.S3ObjectSummary;

If you are just testing the Ceph Object Storage services, consider using HTTP protocol instead of HTTPS protocol.

First, import the ClientConfiguration and Protocol classes.

  1. import com.amazonaws.ClientConfiguration;
  2. import com.amazonaws.Protocol;

Then, define the client configuration, and add the client configuration as an argument for the S3 client.

  1. AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
  2. ClientConfiguration clientConfig = new ClientConfiguration();
  3. clientConfig.setProtocol(Protocol.HTTP);
  4. AmazonS3 conn = new AmazonS3Client(credentials, clientConfig);
  5. conn.setEndpoint("endpoint.com");

Creating a Connection

This creates a connection so that you can interact with the server.

  1. String accessKey = "insert your access key here!";
  2. String secretKey = "insert your secret key here!";
  3. AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
  4. AmazonS3 conn = new AmazonS3Client(credentials);
  5. conn.setEndpoint("objects.dreamhost.com");

Listing Owned Buckets

This gets a list of Buckets that you own. This also prints out the bucket name and creation date of each bucket.

  1. List<Bucket> buckets = conn.listBuckets();
  2. for (Bucket bucket : buckets) {
  3. System.out.println(bucket.getName() + "\t" +
  4. StringUtils.fromDate(bucket.getCreationDate()));
  5. }

The output will look something like this:

  1. mahbuckat1 2011-04-21T18:05:39.000Z
  2. mahbuckat2 2011-04-21T18:05:48.000Z
  3. mahbuckat3 2011-04-21T18:07:18.000Z

Creating a Bucket

This creates a new bucket called my-new-bucket

  1. Bucket bucket = conn.createBucket("my-new-bucket");

Listing a Bucket’s Content

This gets a list of objects in the bucket. This also prints out each object’s name, the file size, and last modified date.

  1. ObjectListing objects = conn.listObjects(bucket.getName());
  2. do {
  3. for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
  4. System.out.println(objectSummary.getKey() + "\t" +
  5. objectSummary.getSize() + "\t" +
  6. StringUtils.fromDate(objectSummary.getLastModified()));
  7. }
  8. objects = conn.listNextBatchOfObjects(objects);
  9. } while (objects.isTruncated());

The output will look something like this:

  1. myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
  2. myphoto2.jpg 262518 2011-08-08T21:38:01.000Z

Deleting a Bucket

Note

The Bucket must be empty! Otherwise it won’t work!

  1. conn.deleteBucket(bucket.getName());

Forced Delete for Non-empty Buckets

Attention

not available

Creating an Object

This creates a file hello.txt with the string "Hello World!"

  1. ByteArrayInputStream input = new ByteArrayInputStream("Hello World!".getBytes());
  2. conn.putObject(bucket.getName(), "hello.txt", input, new ObjectMetadata());

Change an Object’s ACL

This makes the object hello.txt to be publicly readable, and secret_plans.txt to be private.

  1. conn.setObjectAcl(bucket.getName(), "hello.txt", CannedAccessControlList.PublicRead);
  2. conn.setObjectAcl(bucket.getName(), "secret_plans.txt", CannedAccessControlList.Private);

Download an Object (to a file)

This downloads the object perl_poetry.pdf and saves it in /home/larry/documents

  1. conn.getObject(
  2. new GetObjectRequest(bucket.getName(), "perl_poetry.pdf"),
  3. new File("/home/larry/documents/perl_poetry.pdf")
  4. );

Delete an Object

This deletes the object goodbye.txt

  1. conn.deleteObject(bucket.getName(), "goodbye.txt");

Generate Object Download URLs (signed and unsigned)

This generates an unsigned download URL for hello.txt. This works because we made hello.txt public by setting the ACL above. This then generates a signed download URL for secret_plans.txt that will work for 1 hour. Signed download URLs will work for the time period even if the object is private (when the time period is up, the URL will stop working).

Note

The java library does not have a method for generating unsigned URLs, so the example below just generates a signed URL.

  1. GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket.getName(), "secret_plans.txt");
  2. System.out.println(conn.generatePresignedUrl(request));

The output will look something like this:

  1. https://my-bucket-name.objects.dreamhost.com/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX