User guide
}
FileStream s = new FileStream( "s3Object.txt", FileMode.Create );
StreamWriter writer = new StreamWriter( s );
writer.WriteLine( responseBody );
}
catch (AmazonS3Exception s3Exception) {
Console.WriteLine(s3Exception.Message, s3Exception.InnerException);
}
} // main
} // class
} // namespace
You can test this program, by filling in your own credentials in an App.config. The following example
shows the lines you would need to edit.
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="AWSProfileName" value="profile_name"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
You should also specify the name of an Amazon S3 bucket and text object associated with your account.
static string bucketName = "text-content";
static string keyName = "text-object.txt";
For instructions on how to create an Amazon S3 bucket and upload an object, go to the Amazon Simple
Storage Service Getting Started Guide.
After you have configured the program for your environment (credentials and target object to retrieve),
you can compile and run it.You can compile the program in Visual Studio or from a Visual Studio Command
Prompt. Here is a sample command line for compiling the program (here called get-object.cs).You
will need to adjust the path to the AWS assembly (AWSSDK.dll) to match the location of the assembly
on your computer. When you run the program, the assembly should be in the General Assembly Cache
(GAC) or in the same directory as the program.
csc get-object.cs /r:c:\aws-sdk-net\bin\AWSSDK.dll
Update the Sample Program for IAM Roles for Amazon EC2
Instances
Our next stage is to update this program to run from an Amazon EC2 instance using IAM roles for Amazon
EC2 instances. Here are the high-level steps.
1. Create the Role
2. Launch an Amazon EC2 Instance with the Corresponding Instance Profile
Version v2.0.0
34
AWS SDK for .NET Developer Guide
Walkthrough: Using IAM Roles to Retrieve an Amazon
S3 Object from an Amazon EC2 Instance