Integrating Your App with Amazon Web Services S3

By Jeff Cogswell

If you can write software for the cloud, you’re one step ahead of other job hunters. In particular, one area you want to know about is Amazon Web Services (AWS). It’s huge and sophisticated, yet surprisingly easy to integrate into applications. Let’s look at how you can program AWS with Microsoft C# and Visual Studio. Specifically we’ll look at how to store data in the AWS cloud storage called S3.

Cloud Computing with AWSTo get started, you have to sign up for an AWS account and pick up a private key for use in your program. Only solutions that hold this key will be able to save to and retrieve data from S3. Second, you’ll need to configure your application in Visual Studio to use AWS.

Here’s a step-by-step approach.

Starting Out

Head over to http://aws.amazon.com and click the “Sign Up” button. Follow the steps for registering. You’ll need to have your phone handy — an automated system will call you — and you’ll have to enter a PIN. This is so that Amazon can verify that you’re actually a human.

Once you’re verified, sign into your account and go over to the console. You can find it by floating your mouse over the “My Account/Console” drop down in the upper right, then clicking “AWS Management Console.” On the console, you’ll see a list of all the services available. Find S3 and click on it.

At that point, the main S3 page will open. S3 is where you store your files in the equivalent of folders, called “Buckets.” Click “Create Bucket” and you’ll see the screen shown below. Create a name for your bucket in all lower case. Here’s the rub: The name must be unique among Amazon users. One way to realize that is to include your user name along with the bucket name. Leave the “Region” alone. It will default to one for you. (Amazon maintains several data centers across the planet grouped into geographic regions, and this is specifying where your buckets will be stored.) Then click “Create.”

AWS Create Bucket Screen

For this example, we’re going to manually upload a file to our bucket and read it from our C# program. So, next we’ll upload the file. You’ll now see a screen divided in half with the left half showing your buckets and the right-half showing information about any bucket you click on. (Click on the blue area around the bucket to see information about it. Click on the bucket name to manage it.)

AWS Bucket Info Screen

Go ahead and click on the bucket name. You’ll see a screen telling you the bucket is empty. On the upper left, click the “Upload” button. A window will open, and from there you can upload your files. This screen offers a couple of different methods you can use, one of which requires you to have Java installed. Click “Add Files” and find a file you’d like to store. For this example, you can use any file, such as an image. Just make sure it’s not too big, because you don’t want to wait forever for it to upload. Once you’re ready, click “Start Upload.” Here’s the upload screen:

AWS Upload Screen

Once uploaded, the file is stored in S3 and you should see it listed on the left side of the bucket information page.

That’s it. Now let’s head over to Visual Studio.

Working in Visual Studio

Create a new C# console application. Here, using Visual Studio 2012, I’m calling my project AWSTest.

Create New C# Project Screen in VS

Make sure you have installed the NuGet Package Installer. You can find instructions here. NuGet is a vital tool for developing applications with Visual Studio and if you don’t already have it, you’ll definitely want to get it, not just for working with S3 but for other projects as well.

After you’ve created your project, go to the Solution Explorer and right-click on the References folder. In the resulting pop-up menu, click “Manage NuGet Packages.” The NuGet window will open. In the upper right box, where it says Search Online, type in “Amazon Web Services.” You’ll see a list of packages related to that name.

NuGet Window

Click on “AWS SDK for .NET.” Note in the right-hand box that it says “Created by: Amazon Web Services,” then click the “Install” button that appears to the right of it. Another pop-up box will appear telling you that the library is installing. Once finished, click “Close” in the lower right corner. You now have the AWS SDK added to your program. Let’s try it out.

Before you proceed, you’ll need a security key from Amazon. Go back to the management console in the Web browser. Click your name, found in the upper right corner, and click “Security Credentials” in the drop-down.

A new screen will open. You’ll probably see a message about accessing with root credentials. For now, just click “Continue to Security Credentials.” Click on the little plus symbol next to Access Keys, then click “Create New Access Key.”

AWS Creat Access Key Screen

As soon as you do so, you’ll see a pop-up confirming that your key has been created. Click “Download Key File” to save it to your computer. Then click “Show Secret Access Key.” You’ll see an Access Key ID and a Secret Access Key. Leave this window open because you’ll need to reference it shortly. Then head back over to Visual Studio.

Once there, right-click on your project name in Solution Explorer. In the popup menus, click “Add -> New Item.” In the resulting dialogue box, find “Application Configuration File” and click on it. Leave the name as-is (it should be App.config) and click “Add.” A new XML file will be created and added to your project.

Now let’s add the key information. Inside the outer element called Configuration, add an element called appSettings. Inside that, add two elements called “add,” like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="AWSAccessKey" value="1234567" />
<add key="AWSSecretKey" value="abcdefghijklmnopqrstuvwxyz" />
</appSettings>
</configuration>

Go over to the browser window, select and copy the Access Key ID and paste it in the first of the add keys, over the part where I put “1234567” in this sample. Next, go to the browser, grab the secret access key, and copy it over where I put all the letters of the alphabet in the sample code above. Now save your file and close it.

For this example, we’re simply going to download the file from Amazon’s S3 servers. Though it’s only a download, it will show you the process for working with AWS in general.

In Visual Studio, open the Program.cs file. At the top, under the existing “using” lines, add the following:

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System.IO;

Then, add the following code inside your main function:

AmazonS3 client;

using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.USWest2)) {
GetObjectRequest objrequest = new GetObjectRequest();
objrequest.BucketName = "mybucketdicenews";
objrequest.Key = "aws_createbucket.png";
using (GetObjectResponse response = client.GetObject(objrequest)) {
response.WriteResponseStreamToFile("aws_createbucket.png");
}
}

Replace “mybucketdicenews” with the name of your bucket, and “aws_createbucket.png” with the name of the file you uploaded. Then compile and run the program by pressing “Ctrl+F5.” You’ll see a console window open. In a moment you’ll see the words “Press any key to continue.” That means you program is finished.

With either a command prompt or through Windows Explorer, head over to the folder where you created your project (if you didn’t manually choose a location, it will be in the folder c:\users\jeff\Documents\Visual Studio 2012\Projects, replacing “jeff” with your username). Navigate down into the folder called “bin,” and under that the one called “debug.” You’ll see the file you created. If it’s an image, you can double-click and open it with your image viewer. It worked!

Conclusion

There’s more to using AWS, of course, but the general procedure is the same. Note that you don’t need to keep creating new keys. Once you have yours, you can re-use it in your applications.

There are many services in AWS, including EC2, which is where you allocate cloud servers, DynamoDB, a modern NoSQL database, and many more. Once you master the technique shown here, you’ll be able to easily code for them as well. You’ll be able to give your application the cloud capabilities employers want to see on your resume.

Image: Amazon

Post a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>