Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrades for AWS SDK for Java version 2 #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,38 @@

<groupId>com.amazonaws.samples</groupId>
<artifactId>aws-java-sample</artifactId>
<version>1.0</version>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>aws-java-sample</name>
<url>http://aws.amazon.com/sdkforjava</url>

<properties>
<maven.compiler.source>${maven.compiler.target}</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.0.0-preview-10</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.9.6</version>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>

Expand Down
88 changes: 36 additions & 52 deletions src/main/java/com/amazonaws/samples/S3Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,17 @@
*/
package com.amazonaws.samples;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;

import java.io.*;
import java.util.UUID;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
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.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;

/**
* This sample demonstrates how to make basic requests to Amazon S3 using
* the AWS SDK for Java.
Expand All @@ -62,9 +49,7 @@ public static void main(String[] args) throws IOException {
* aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
*/

AmazonS3 s3 = new AmazonS3Client();
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
s3.setRegion(usWest2);
S3Client s3 = S3Client.builder().region(Region.US_WEST_2).build();

String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
String key = "MyObjectKey";
Expand All @@ -83,14 +68,14 @@ public static void main(String[] args) throws IOException {
* keep your data closer to your applications or users.
*/
System.out.println("Creating bucket " + bucketName + "\n");
s3.createBucket(bucketName);
s3.createBucket(b -> b.bucket(bucketName));

/*
* List the buckets in your account
*/
System.out.println("Listing buckets");
for (Bucket bucket : s3.listBuckets()) {
System.out.println(" - " + bucket.getName());
for (Bucket bucket : s3.listBuckets().buckets()) {
System.out.println(" - " + bucket.name());
}
System.out.println();

Expand All @@ -103,7 +88,7 @@ public static void main(String[] args) throws IOException {
* specific to your applications.
*/
System.out.println("Uploading a new object to S3 from a file\n");
s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
s3.putObject(b -> b.bucket(bucketName).key(key), RequestBody.fromFile(createSampleFile()));

/*
* Download an object - When you download an object, you get all of
Expand All @@ -118,9 +103,15 @@ public static void main(String[] args) throws IOException {
* ETags, and selectively downloading a range of an object.
*/
System.out.println("Downloading an object");
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
displayTextInputStream(object.getObjectContent());
try (ResponseInputStream<GetObjectResponse> object = s3.getObject(b -> b.bucket(bucketName).key(key))) {
try {
System.out.println("Content-Type: " + object.response().contentType());
displayTextInputStream(object);
} catch (IOException e) {
object.abort();
throw e;
}
}

/*
* List objects in your bucket by prefix - There are many options for
Expand All @@ -131,12 +122,9 @@ public static void main(String[] args) throws IOException {
* additional results.
*/
System.out.println("Listing objects");
ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix("My"));
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " +
"(size = " + objectSummary.getSize() + ")");
ListObjectsResponse objectListing = s3.listObjects(b -> b.bucket(bucketName).prefix("My"));
for (S3Object objectSummary : objectListing.contents()) {
System.out.println(" - " + objectSummary.key() + " " + "(size = " + objectSummary.size() + ")");
}
System.out.println();

Expand All @@ -145,25 +133,25 @@ public static void main(String[] args) throws IOException {
* there is no way to undelete an object, so use caution when deleting objects.
*/
System.out.println("Deleting an object\n");
s3.deleteObject(bucketName, key);
s3.deleteObject(b -> b.bucket(bucketName).key(key));

/*
* Delete a bucket - A bucket must be completely empty before it can be
* deleted, so remember to delete any objects from your buckets before
* you try to delete them.
*/
System.out.println("Deleting bucket " + bucketName + "\n");
s3.deleteBucket(bucketName);
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which means your request made it "
s3.deleteBucket(b -> b.bucket(bucketName));
} catch (AwsServiceException ase) {
System.out.println("Caught an AwsServiceException, which means your request made it "
+ "to Amazon S3, but was rejected with an error response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means the client encountered "
System.out.println("HTTP Status Code: " + ase.statusCode());
System.out.println("AWS Error Code: " + ase.errorCode());
System.out.println("Error Type: " + ase.errorType());
System.out.println("Request ID: " + ase.requestId());
} catch (SdkClientException ace) {
System.out.println("Caught an SdkClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with S3, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
Expand All @@ -175,8 +163,6 @@ public static void main(String[] args) throws IOException {
* to Amazon S3
*
* @return A newly created temporary file with text data.
*
* @throws IOException
*/
private static File createSampleFile() throws IOException {
File file = File.createTempFile("aws-java-sdk-", ".txt");
Expand All @@ -198,8 +184,6 @@ private static File createSampleFile() throws IOException {
*
* @param input
* The input stream to display as text.
*
* @throws IOException
*/
private static void displayTextInputStream(InputStream input) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
Expand Down