This article lists several Java code examples to interact with DreamObjects.
Setup
The following examples may require some or all of the following java classes to be imported:
import java.io.ByteArrayInputStream; import java.io.File; import java.util.List; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.util.StringUtils; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.Bucket; import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.S3ObjectSummary;
Creating a Connection
Create a connection so you can interact with the server.
String accessKey = "insert your access key here!"; String secretKey = "insert your secret key here!"; AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); AmazonS3 conn = new AmazonS3Client(credentials); conn.setEndpoint("objects-us-east-1.dream.io");
Listing Owned Buckets
Get a list of Buckets you own. This also prints out the bucket name and creation date of each bucket.
List buckets = conn.listBuckets(); for (Bucket bucket : buckets) { System.out.println(bucket.getName() + "\t" + StringUtils.fromDate(bucket.getCreationDate())); }
The output looks something like this:
mahbuckat1 2011-04-21T18:05:39.000Z mahbuckat2 2011-04-21T18:05:48.000Z mahbuckat3 2011-04-21T18:07:18.000Z
Creating a Bucket
Create a new bucket called my-new-bucket.
Bucket bucket = conn.createBucket("my-new-bucket");
Listing a Bucket’s Content
Get a list of objects in the bucket. This also prints out each object’s name, the file size, and last modified date.
ObjectListing objects = conn.listObjects(bucket.getName()); do { for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) { System.out.println(objectSummary.getKey() + "\t" + ObjectSummary.getSize() + "\t" + StringUtils.fromDate(objectSummary.getLastModified())); } objects = conn.listNextBatchOfObjects(objects); } while (objects.isTruncated());
The output looks something like this:
myphoto1.jpg 251262 2011-08-08T21:35:48.000Z myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
Deleting a Bucket
The Bucket must be empty, otherwise it won’t work.
conn.deleteBucket(bucket.getName());
Forced Delete for Non-empty Buckets
not available
Creating an Object
Create a file hello.txt
with the string "Hello World!"
ByteArrayInputStream input = new ByteArrayInputStream("Hello World!".getBytes()); conn.putObject(bucket.getName(), "hello.txt", input, new ObjectMetadata());
Change an Object’s ACL
Make the object hello.txt
publicly readable and secret_plans.txt
to be private.
conn.setObjectAcl(bucket.getName(), "hello.txt", CannedAccessControlList.PublicRead); conn.setObjectAcl(bucket.getName(), "secret_plans.txt", CannedAccessControlList.Private);
Download an Object (to a file)
Download the object perl_poetry.pdf
and save it in /home/username/documents.
Make sure to change username to your Shell user.
conn.getObject( new GetObjectRequest(bucket.getName(), "perl_poetry.pdf"), new File("/home/username/documents/perl_poetry.pdf") );
Delete an Object
Delete the object goodbye.txt
conn.deleteObject(bucket.getName(), "goodbye.txt");
Generate Object Download URLs (signed and unsigned)
Generate an unsigned download URL for hello.txt
. This works because hello.txt
was made 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).
The Java library does not have a method for generating unsigned URLs, so the example below just generates a signed URL.
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket.getName(), "secret_plans.txt"); System.out.println(conn.generatePresignedUrl(request));
The output will look something like this:
https://my-bucket-name.objects-us-east-1.dream.io/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX