9 The following examples may require some or all of the following java
10 classes to be imported:
14 import java.io.ByteArrayInputStream;
16 import java.util.List;
17 import com.amazonaws.auth.AWSCredentials;
18 import com.amazonaws.auth.BasicAWSCredentials;
19 import com.amazonaws.util.StringUtils;
20 import com.amazonaws.services.s3.AmazonS3;
21 import com.amazonaws.services.s3.AmazonS3Client;
22 import com.amazonaws.services.s3.model.Bucket;
23 import com.amazonaws.services.s3.model.CannedAccessControlList;
24 import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
25 import com.amazonaws.services.s3.model.GetObjectRequest;
26 import com.amazonaws.services.s3.model.ObjectListing;
27 import com.amazonaws.services.s3.model.ObjectMetadata;
28 import com.amazonaws.services.s3.model.S3ObjectSummary;
31 If you are just testing the Ceph Object Storage services, consider
32 using HTTP protocol instead of HTTPS protocol.
34 First, import the ``ClientConfiguration`` and ``Protocol`` classes.
38 import com.amazonaws.ClientConfiguration;
39 import com.amazonaws.Protocol;
42 Then, define the client configuration, and add the client configuration
43 as an argument for the S3 client.
47 AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
49 ClientConfiguration clientConfig = new ClientConfiguration();
50 clientConfig.setProtocol(Protocol.HTTP);
52 AmazonS3 conn = new AmazonS3Client(credentials, clientConfig);
53 conn.setEndpoint("endpoint.com");
59 This creates a connection so that you can interact with the server.
63 String accessKey = "insert your access key here!";
64 String secretKey = "insert your secret key here!";
66 AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
67 AmazonS3 conn = new AmazonS3Client(credentials);
68 conn.setEndpoint("objects.dreamhost.com");
74 This gets a list of Buckets that you own.
75 This also prints out the bucket name and creation date of each bucket.
79 List<Bucket> buckets = conn.listBuckets();
80 for (Bucket bucket : buckets) {
81 System.out.println(bucket.getName() + "\t" +
82 StringUtils.fromDate(bucket.getCreationDate()));
85 The output will look something like this::
87 mahbuckat1 2011-04-21T18:05:39.000Z
88 mahbuckat2 2011-04-21T18:05:48.000Z
89 mahbuckat3 2011-04-21T18:07:18.000Z
95 This creates a new bucket called ``my-new-bucket``
99 Bucket bucket = conn.createBucket("my-new-bucket");
102 Listing a Bucket's Content
103 --------------------------
104 This gets a list of objects in the bucket.
105 This also prints out each object's name, the file size, and last
110 ObjectListing objects = conn.listObjects(bucket.getName());
112 for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
113 System.out.println(objectSummary.getKey() + "\t" +
114 objectSummary.getSize() + "\t" +
115 StringUtils.fromDate(objectSummary.getLastModified()));
117 objects = conn.listNextBatchOfObjects(objects);
118 } while (objects.isTruncated());
120 The output will look something like this::
122 myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
123 myphoto2.jpg 262518 2011-08-08T21:38:01.000Z
130 The Bucket must be empty! Otherwise it won't work!
134 conn.deleteBucket(bucket.getName());
137 Forced Delete for Non-empty Buckets
138 -----------------------------------
146 This creates a file ``hello.txt`` with the string ``"Hello World!"``
150 ByteArrayInputStream input = new ByteArrayInputStream("Hello World!".getBytes());
151 conn.putObject(bucket.getName(), "hello.txt", input, new ObjectMetadata());
154 Change an Object's ACL
155 ----------------------
157 This makes the object ``hello.txt`` to be publicly readable, and
158 ``secret_plans.txt`` to be private.
162 conn.setObjectAcl(bucket.getName(), "hello.txt", CannedAccessControlList.PublicRead);
163 conn.setObjectAcl(bucket.getName(), "secret_plans.txt", CannedAccessControlList.Private);
166 Download an Object (to a file)
167 ------------------------------
169 This downloads the object ``perl_poetry.pdf`` and saves it in
170 ``/home/larry/documents``
175 new GetObjectRequest(bucket.getName(), "perl_poetry.pdf"),
176 new File("/home/larry/documents/perl_poetry.pdf")
183 This deletes the object ``goodbye.txt``
187 conn.deleteObject(bucket.getName(), "goodbye.txt");
190 Generate Object Download URLs (signed and unsigned)
191 ---------------------------------------------------
193 This generates an unsigned download URL for ``hello.txt``. This works
194 because we made ``hello.txt`` public by setting the ACL above.
195 This then generates a signed download URL for ``secret_plans.txt`` that
196 will work for 1 hour. Signed download URLs will work for the time
197 period even if the object is private (when the time period is up, the
198 URL will stop working).
201 The java library does not have a method for generating unsigned
202 URLs, so the example below just generates a signed URL.
206 GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket.getName(), "secret_plans.txt");
207 System.out.println(conn.generatePresignedUrl(request));
209 The output will look something like this::
211 https://my-bucket-name.objects.dreamhost.com/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX