User guide

Table Of Contents
The following walkthrough uses a sample program that retrieves an object from Amazon S3 using the
AWS credentials that you've configured. Next, we create an IAM role to provide the AWS credentials.
Finally, we launch an instance with an IAM role that provides the AWS credentials to the sample program
running on the instance.
Walkthrough
Create a Sample that Retrieves an Object from Amazon S3 (p. 48)
Create an IAM Role (p. 49)
Launch an EC2 Instance and Specify the IAM Role (p. 49)
Run the Sample Program on the EC2 Instance (p. 50)
Create a Sample that Retrieves an Object from
Amazon S3
The following sample code retrieves an object from Amazon S3. It requires a text file in an Amazon S3
bucket that you have access to. For more information about creating an Amazon S3 bucket and uploading
an object, see the Amazon Simple Storage Service Getting Started Guide. It also requires AWS credentials
that provide you with access to the Amazon S3 bucket. For more information, see Configuring AWS
Credentials (p. 9).
using System;
using System.Collections.Specialized;
using System.IO;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
namespace s3.amazon.com.docsamples.retrieveobject
{
class S3Sample
{
static string bucketName = "bucket-name";
static string keyName = "folder/file-name.txt";
static IAmazonS3 client;
public static void Main(string[] args) {
string responseBody = "";
try {
using (client = new AmazonS3Client()) {
Console.WriteLine("Retrieving (GET) an object");
GetObjectRequest request = new GetObjectRequest() {
BucketName = bucketName,
Key = keyName
};
using (GetObjectResponse response = client.GetObject(request))
using (Stream responseStream = response.ResponseStream)
using (StreamReader reader = new StreamReader(responseStream))
Version v2.0.0
48
AWS SDK for .NET Developer Guide
Create a Sample that Retrieves an Object from Amazon
S3