Turbocharge Your ASP.NET Site With Amazon S3 (VB.NET)

Sample Code & Libraries>Turbocharge Your ASP.NET Site With Amazon S3 (VB.NET)
Community Contributed Software

  • Amazon Web Services provides links to these packages as a convenience for our customers, but software not authored by an "@AWS" account has not been reviewed or screened by AWS.
  • Please review this software to ensure it meets your needs before using it.

A VB.NET version of code meant to accompany the video by the same title.

Details

Submitted By: Mike Culver
AWS Products Used: Amazon S3
Language(s): VB.NET
Created On: June 20, 2007 6:07 PM GMT
Last Updated: September 21, 2008 9:29 PM GMT
Download

About This Sample

This is a VB.NET version of previously-published sample code, meant to accompany a screencast by the same name. Imagine that your community photo-sharing site runs on your home server connected to a slow DSL line. Of course, photos are bandwidth intensive, which will create a poor user experience on a DSL connection. In this code sample, photos are initially uploaded to your server, but then reposted to Amazon S3 using the webmaster's credentials. The result is a hybrid site, where the local server runs ASP.NET but bandwidth-intensive content is delivered straight out of Amazon's data centers.

Note that there are two projects included in the compressed file: One is the "before" snapshot, and the other is "after" (with Amazon S3 integrated). The notes below assume that you're using the "after" version.

Prerequisites

  • You must have a valid Amazon Web Services developer account.
  • You must be signed up to use Amazon Simple Storage Service, also known as Amazon S3.
  • We assume that you have Microsoft Visual Studio 2005 Professional Edition installed as an IDE. You can build this demo with any text editor; however we do not tell you how to compile the application from a command line.
  • You'll need to use the class libraries provided in this download. They're almost the same as the "regular" class library files in the resource center, with several small tweaks for the convenience of this sample.

Running the Sample

After extracting the code from the zip file, choose "Open Website" in Visual Studio 2005 and navigate to the directory where you extracted the code to. Visual Studio will automatically create a project file for the directory.

This sample uses an Access database to store a list of photos. You'll need to give the ASPNET machine account read/write access to Images.mdb file. Right-click on the database file to set security permissions.

There are two ways to run the sample. The first approach is to simply pick a bucket name in Amazon S3 and test the code out. The other approach allows you to create a subdomain on your site such as "photos.mydomain.com".

Option A: Choose a Unique Bucket Name
Let's assume you chose "myname-photos" as a bucket name. Bucket names are case sensitive.

  1. Create an Amazon S3 bucket to match the name you are using. Remember that bucket names must be unique to you. So "test" won't work, because someone else already used it. The easiest way to create a bucket is with a Firefox Extension known as S3 Organizer, which you can download from www.rjonna.com.
  2. Edit Web.config to reflect your Amazon S3 credentials in the appSettings section.
  3. Also Edit web.config to reflect the bucket name that you want to store your uploads in.
    1. Edit PhotoGalleryBaseUrl to reflect the full path to your bucket. public string PhotoGalleryBaseUrl = "http://s3.amazonaws.com/myname-photos/"; Note the trailing slash!
    2. Edit ImageBucketName to reflect this name. private const string ImageBucketName = "myname-photos"
  4. Test the site to make certain everything works. To test, open the project in Visual Studio 2005, navigate to default.aspx, and press F5.
  5. Once everything works, close default.aspx to terminate the session. Then open Encryption.aspx and press F5 to run it. This will encrypt the section of web.config that contains your Amazon S3 credentials. Before running this page, right-click on web.config and modify permissions to give the ASPNET machine account modify access to web.config file for Encryption.aspx to work.

Option B: Choose a Unique Bucket Name That Reflects a Subdomain in Your Domain
Let's assume you own a domain known as "mydomain.com". You want users to think that your server is delivering photos from a subdomain known as "photos.mydomain.com". Accordingly you will create a bucket name byt the name of "photos.mydomain.com". Use all lower case letters in the bucket name, because this is how DNS will handle the name, and spell it all the way out as shown here--including all the periods.

  1. In your DNS server, create a CNAME entry that maps "photos.mydomain.com" to "s3.amazonaws.com". Depending on your DNS software, you'll probably just map "photos" (without "mydomain.com") to s3.amazonaws.com. That's because your DNS server probably already knows what domain name you mean. When Amazon S3 receives a redirect looking for photos.mydomain.com, it will look for a bucket by that name.
  2. Create an Amazon S3 bucket to match the name you are using. Remember that bucket names must be unique to you. So "test" won't work, because someone else already used it. The easiest way to create a bucket is with a Firefox Extension known as S3 Organizer, which you can download from www.rjonna.com.
  3. Edit Web.config to reflect your Amazon S3 credentials in the appSettings section.
  4. Also edit web.config to reflect the bucket name that you want to store your uploads in.
    1. Edit PhotoGalleryBaseUrl to reflect the full path to your bucket. public string PhotoGalleryBaseUrl = "http://photos.mydomain.com/"; Note the trailing slash!
    2. Edit ImageBucketName to reflect this name. private const string ImageBucketName = "photos.mydomain.com"
  5. Test the site to make certain everything works. To test, open the project in Visual Studio 2005, navigate to default.aspx, and press F5.
  6. Once everything works, close default.aspx to terminate the session. Then open Encryption.aspx and press F5 to run it. This will encrypt the section of web.config that contains your Amazon S3 credentials. Before running this page, right-click on web.config and modify permissions to give the ASPNET machine account modify access to web.config file for Encryption.aspx to work.
©2014, Amazon Web Services, Inc. or its affiliates. All rights reserved.