User guide

{
System.out.println("Downloading an object");
S3Object s3object = s3Client.getObject(
new GetObjectRequest(bucketName, key));
displayTextInputStream(s3object.getObjectContent());
}
catch(AmazonServiceException ase)
{
System.out.println( "AmazonServiceException" );
}
catch(AmazonClientException ace)
{
System.out.println( "AmazonClientException" );
}
}
private static void displayTextInputStream(InputStream input) throws IOExcep
tion
{
// Read one text line at a time and display.
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
while(true)
{
String line = reader.readLine();
if(line == null) break;
System.out.println( " " + line );
}
System.out.println();
}
}
You'll also need an Ant build script to build and run your application. Open a new file in the same directory
as GetS3Object.java and call it build.xml. Add the following lines to the file:
<project name="Get Amazon S3 Object" default="run" basedir=".">
<path id="aws.java.sdk.classpath">
<fileset dir="./lib" includes="**/*.jar"/>
<fileset dir="./third-party" includes="**/*.jar"/>
<pathelement location="lib"/>
<pathelement location="."/>
</path>
<target name="build">
<javac debug="true"
includeantruntime="false"
srcdir="."
destdir="."
classpathref="aws.java.sdk.classpath"/>
</target>
<target name="run" depends="build">
<java classname="GetS3Object" classpathref="aws.java.sdk.classpath"
fork="true"/>
</target>
</project>
Version v1.0.0
34
AWS SDK for Java Developer Guide
Using IAM Roles for EC2 Instances