Intelliwins Blog

Anything you wanna know about?

Amazon S3 with C# Web Forms

by Admin 25. December 2015 16:39

AWS S3 Storage is one of the most cost effective and efficient available to right now. If you are dealing with large files especially video and audio files which you need to add into website or user RTMP streaming then AWS S3 with Cloud front CDN is one of the best solution available options for a cost effective solution. There are other option may be to hire a server with SSD and loads of RAM and Processor Cores but as I mentioned it won’t be a cost effective one.

I personally feel that documentation about amazon AWS integration is not sufficient , that is why I feel to write down this article , which will cover creating a Amazon AWS account (Which also include 5 GB of space for free of cost in AWS S3), Generating API key (Which will be used while integrating SDK) , Adding Amazon SDK (nuget is best option), Configuring and creating amazon S3 bucket, Writing C# codes to upload file from file upload control to S3 bucket.

Creating Amazon AWS Account

This quite straight, I think most of us already have an amazon account thanks to e commerce revolution or you can create one very easily.

Go to

Then sign in with your amazon id and password. Accept few terms and condition. You aws console is ready

Generating API key

After you log into Amazon AWS console click on your name in top right . Then select Security Credential. In the pop up dialog select Continue with Security Credential. Then click on Access Keys and Click on the button Create New Access Keys. There you can also download the key file. Which is basically a CSV file containing Access Key and Access Secrete Key.

Creating Amazon Bucket

From Top Left Services menu select S3. Then Click on Create Bucket Button on top of the page and give a name to your bucket and select the region of your bucket. Your S3 bucket will hold all your data.

Adding Amazon SDK for

In my view best and easier option is add Amazon SDK is via VS Package Manager Nuget . To Add SDK via nuget.

PM > Install-Package AWSSDK.Core -Version

And then you need to a AWS S3 SDK

PM > Install-Package AWSSDK.S3 -Version

All required library has been add to your project.

Writing Codes in C# to With File upload Control to Upload Files AWS S3

First of all if you are uploading files to amazon S3 then I guess you are dealing with large files or to be precise large media files like image, videos or audio clips. We need to make few changes in the web.config file to enable large file upload without connect being reset . For that we need to add following lines in web.config under .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Amazon;
using Amazon.S3;
using Amazon.S3.Transfer;
/// Summary description for AmazonUploader
public class AmazonUploader
	    public bool sendMyFileToS3(System.IO.Stream localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
        // input explained :
        // localFilePath = we will use a file stream , instead of path
        // bucketName : the name of the bucket in S3 ,the bucket should be already created
        // subDirectoryInBucket : if this string is not empty the file will be uploaded to
            // a subdirectory with this name
        // fileNameInS3 = the file name in the S3

        // create an instance of IAmazonS3 class ,in my case i choose RegionEndpoint.EUWest1
        // you can change that to APNortheast1 , APSoutheast1 , APSoutheast2 , CNNorth1
        // SAEast1 , USEast1 , USGovCloudWest1 , USWest1 , USWest2 . this choice will not
        // store your file in a different cloud storage but (i think) it differ in performance
        // depending on your location

            IAmazonS3 client = new AmazonS3Client("Your Access Key", "Your Secrete Key", Amazon.RegionEndpoint.USWest2);

        // create a TransferUtility instance passing it the IAmazonS3 created in the first step
        TransferUtility utility = new TransferUtility(client);
        // making a TransferUtilityUploadRequest instance
        TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();

        if (subDirectoryInBucket == "" || subDirectoryInBucket == null)
            request.BucketName = bucketName; //no subdirectory just bucket name
        {   // subdirectory and bucket name
            request.BucketName = bucketName + @"/" + subDirectoryInBucket;
        request.Key = fileNameInS3 ; //file name up in S3
        //request.FilePath = localFilePath; //local file name
        request.InputStream = localFilePath;
        request.CannedACL = S3CannedACL.PublicReadWrite;
        utility.Upload(request); //commensing the transfer

        return true; //indicate that the file was sent

Then Create a Page Upload.aspx

Code for Upload.aspx


Code for Upload.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
public partial class upload : System.Web.UI.Page
    AmazonUploader obj = new AmazonUploader();
    protected void Page_Load(object sender, EventArgs e)
  protected void Button1_Click(object sender, EventArgs e)
Stream st=FileUpload1.PostedFile.InputStream;
obj1.sendMyFileToS3(st, "BucketName", "", FileName);


Now it is done !!!

Buy the source code from following link


Tags: , , , , | AWS | AWS S3 | Tips and tricks

Add comment

  • Comment
  • Preview

Brit Colony, Bhubaneswar,India +91 94384 80191 info@

About Us

Intelliwins mission is to alter the dynamics of the software industry by providing trusted, supportive and quality software development servies to clients that view our partnership as a strategic driver for their success.


Enter your email to join our mailing list