|
| 1 | +/* |
| 2 | + * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | +package com.amazonaws.samples; |
| 16 | + |
| 17 | +import java.io.BufferedReader; |
| 18 | +import java.io.File; |
| 19 | +import java.io.FileOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.io.InputStreamReader; |
| 23 | +import java.io.OutputStreamWriter; |
| 24 | +import java.io.Writer; |
| 25 | +import java.util.UUID; |
| 26 | + |
| 27 | +import com.amazonaws.AmazonClientException; |
| 28 | +import com.amazonaws.AmazonServiceException; |
| 29 | +import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider; |
| 30 | +import com.amazonaws.regions.Region; |
| 31 | +import com.amazonaws.regions.Regions; |
| 32 | +import com.amazonaws.services.s3.AmazonS3; |
| 33 | +import com.amazonaws.services.s3.AmazonS3Client; |
| 34 | +import com.amazonaws.services.s3.model.Bucket; |
| 35 | +import com.amazonaws.services.s3.model.GetObjectRequest; |
| 36 | +import com.amazonaws.services.s3.model.ListObjectsRequest; |
| 37 | +import com.amazonaws.services.s3.model.ObjectListing; |
| 38 | +import com.amazonaws.services.s3.model.PutObjectRequest; |
| 39 | +import com.amazonaws.services.s3.model.S3Object; |
| 40 | +import com.amazonaws.services.s3.model.S3ObjectSummary; |
| 41 | + |
| 42 | +/** |
| 43 | + * This sample demonstrates how to make basic requests to Amazon S3 using |
| 44 | + * the AWS SDK for Java. |
| 45 | + * <p> |
| 46 | + * <b>Prerequisites:</b> You must have a valid Amazon Web Services developer |
| 47 | + * account, and be signed up to use Amazon S3. For more information on |
| 48 | + * Amazon S3, see http://aws.amazon.com/s3. |
| 49 | + * <p> |
| 50 | + * <b>Important:</b> Be sure to fill in your AWS access credentials in the |
| 51 | + * AwsCredentials.properties file before you try to run this |
| 52 | + * sample. |
| 53 | + * http://aws.amazon.com/security-credentials |
| 54 | + */ |
| 55 | +public class S3Sample { |
| 56 | + |
| 57 | + public static void main(String[] args) throws IOException { |
| 58 | + /* |
| 59 | + * This credentials provider implementation loads your AWS credentials |
| 60 | + * from a properties file at the root of your classpath. |
| 61 | + * |
| 62 | + * Important: Be sure to fill in your AWS access credentials in the |
| 63 | + * AwsCredentials.properties file before you try to run this |
| 64 | + * sample. |
| 65 | + * http://aws.amazon.com/security-credentials |
| 66 | + */ |
| 67 | + AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider()); |
| 68 | + Region usWest2 = Region.getRegion(Regions.US_WEST_2); |
| 69 | + s3.setRegion(usWest2); |
| 70 | + |
| 71 | + String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); |
| 72 | + String key = "MyObjectKey"; |
| 73 | + |
| 74 | + System.out.println("==========================================="); |
| 75 | + System.out.println("Getting Started with Amazon S3"); |
| 76 | + System.out.println("===========================================\n"); |
| 77 | + |
| 78 | + try { |
| 79 | + /* |
| 80 | + * Create a new S3 bucket - Amazon S3 bucket names are globally unique, |
| 81 | + * so once a bucket name has been taken by any user, you can't create |
| 82 | + * another bucket with that same name. |
| 83 | + * |
| 84 | + * You can optionally specify a location for your bucket if you want to |
| 85 | + * keep your data closer to your applications or users. |
| 86 | + */ |
| 87 | + System.out.println("Creating bucket " + bucketName + "\n"); |
| 88 | + s3.createBucket(bucketName); |
| 89 | + |
| 90 | + /* |
| 91 | + * List the buckets in your account |
| 92 | + */ |
| 93 | + System.out.println("Listing buckets"); |
| 94 | + for (Bucket bucket : s3.listBuckets()) { |
| 95 | + System.out.println(" - " + bucket.getName()); |
| 96 | + } |
| 97 | + System.out.println(); |
| 98 | + |
| 99 | + /* |
| 100 | + * Upload an object to your bucket - You can easily upload a file to |
| 101 | + * S3, or upload directly an InputStream if you know the length of |
| 102 | + * the data in the stream. You can also specify your own metadata |
| 103 | + * when uploading to S3, which allows you set a variety of options |
| 104 | + * like content-type and content-encoding, plus additional metadata |
| 105 | + * specific to your applications. |
| 106 | + */ |
| 107 | + System.out.println("Uploading a new object to S3 from a file\n"); |
| 108 | + s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); |
| 109 | + |
| 110 | + /* |
| 111 | + * Download an object - When you download an object, you get all of |
| 112 | + * the object's metadata and a stream from which to read the contents. |
| 113 | + * It's important to read the contents of the stream as quickly as |
| 114 | + * possibly since the data is streamed directly from Amazon S3 and your |
| 115 | + * network connection will remain open until you read all the data or |
| 116 | + * close the input stream. |
| 117 | + * |
| 118 | + * GetObjectRequest also supports several other options, including |
| 119 | + * conditional downloading of objects based on modification times, |
| 120 | + * ETags, and selectively downloading a range of an object. |
| 121 | + */ |
| 122 | + System.out.println("Downloading an object"); |
| 123 | + S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); |
| 124 | + System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); |
| 125 | + displayTextInputStream(object.getObjectContent()); |
| 126 | + |
| 127 | + /* |
| 128 | + * List objects in your bucket by prefix - There are many options for |
| 129 | + * listing the objects in your bucket. Keep in mind that buckets with |
| 130 | + * many objects might truncate their results when listing their objects, |
| 131 | + * so be sure to check if the returned object listing is truncated, and |
| 132 | + * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve |
| 133 | + * additional results. |
| 134 | + */ |
| 135 | + System.out.println("Listing objects"); |
| 136 | + ObjectListing objectListing = s3.listObjects(new ListObjectsRequest() |
| 137 | + .withBucketName(bucketName) |
| 138 | + .withPrefix("My")); |
| 139 | + for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { |
| 140 | + System.out.println(" - " + objectSummary.getKey() + " " + |
| 141 | + "(size = " + objectSummary.getSize() + ")"); |
| 142 | + } |
| 143 | + System.out.println(); |
| 144 | + |
| 145 | + /* |
| 146 | + * Delete an object - Unless versioning has been turned on for your bucket, |
| 147 | + * there is no way to undelete an object, so use caution when deleting objects. |
| 148 | + */ |
| 149 | + System.out.println("Deleting an object\n"); |
| 150 | + s3.deleteObject(bucketName, key); |
| 151 | + |
| 152 | + /* |
| 153 | + * Delete a bucket - A bucket must be completely empty before it can be |
| 154 | + * deleted, so remember to delete any objects from your buckets before |
| 155 | + * you try to delete them. |
| 156 | + */ |
| 157 | + System.out.println("Deleting bucket " + bucketName + "\n"); |
| 158 | + s3.deleteBucket(bucketName); |
| 159 | + } catch (AmazonServiceException ase) { |
| 160 | + System.out.println("Caught an AmazonServiceException, which means your request made it " |
| 161 | + + "to Amazon S3, but was rejected with an error response for some reason."); |
| 162 | + System.out.println("Error Message: " + ase.getMessage()); |
| 163 | + System.out.println("HTTP Status Code: " + ase.getStatusCode()); |
| 164 | + System.out.println("AWS Error Code: " + ase.getErrorCode()); |
| 165 | + System.out.println("Error Type: " + ase.getErrorType()); |
| 166 | + System.out.println("Request ID: " + ase.getRequestId()); |
| 167 | + } catch (AmazonClientException ace) { |
| 168 | + System.out.println("Caught an AmazonClientException, which means the client encountered " |
| 169 | + + "a serious internal problem while trying to communicate with S3, " |
| 170 | + + "such as not being able to access the network."); |
| 171 | + System.out.println("Error Message: " + ace.getMessage()); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Creates a temporary file with text data to demonstrate uploading a file |
| 177 | + * to Amazon S3 |
| 178 | + * |
| 179 | + * @return A newly created temporary file with text data. |
| 180 | + * |
| 181 | + * @throws IOException |
| 182 | + */ |
| 183 | + private static File createSampleFile() throws IOException { |
| 184 | + File file = File.createTempFile("aws-java-sdk-", ".txt"); |
| 185 | + file.deleteOnExit(); |
| 186 | + |
| 187 | + Writer writer = new OutputStreamWriter(new FileOutputStream(file)); |
| 188 | + writer.write("abcdefghijklmnopqrstuvwxyz\n"); |
| 189 | + writer.write("01234567890112345678901234\n"); |
| 190 | + writer.write("!@#$%^&*()-=[]{};':',.<>/?\n"); |
| 191 | + writer.write("01234567890112345678901234\n"); |
| 192 | + writer.write("abcdefghijklmnopqrstuvwxyz\n"); |
| 193 | + writer.close(); |
| 194 | + |
| 195 | + return file; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Displays the contents of the specified input stream as text. |
| 200 | + * |
| 201 | + * @param input |
| 202 | + * The input stream to display as text. |
| 203 | + * |
| 204 | + * @throws IOException |
| 205 | + */ |
| 206 | + private static void displayTextInputStream(InputStream input) throws IOException { |
| 207 | + BufferedReader reader = new BufferedReader(new InputStreamReader(input)); |
| 208 | + while (true) { |
| 209 | + String line = reader.readLine(); |
| 210 | + if (line == null) break; |
| 211 | + |
| 212 | + System.out.println(" " + line); |
| 213 | + } |
| 214 | + System.out.println(); |
| 215 | + } |
| 216 | + |
| 217 | +} |