9 Jan

Store files in Amazon S3 using AWS SDK in ASP.NET MVC

In this article we will learn how to store files in Amazon S3 using AWS SDK in ASP.NET MVC.

Check here how to Upload Base64 data to S3 using AWS SDK in ASP.NET MVC

Whats is Amazon S3

Amazon S3 (Simple Storage Service) is an online file storage web service offered by Amazon Web Services. The S3 allows uploading, storage and downloading of practically any file or object up to five gigabytes (5 GB) in size.  To use Amazon S3, we must create a bucket to the store data. Each Buckets have the configuration properties like to make the objects accesses public or private.

Step 1: Install Amazon AWS SDK

To install AWS SDK for .NET, run the following command in the Package Manager Console

Install-Package AWSSDK

Step 2:  Get your Access Key ID and Secret Access Key

To use AWS SDK, first you need to get your Access Key ID and Secret Access Key. You can create using the IAM console https://console.aws.amazon.com/iam/home?#home to know more about how create your access keys please check the following documentation at amazon http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html.

Step 3: Create Bucket

Here we can create the bucket using AWS Console.

S3 Management Console

S3 Management Console

Once the bucket is created save the bucket name in the web.config file. Also save your AWSAccessKey and AWSSecretKey in the 
web.config file.

Step 4: Using AWS SDK.Net

Upload a file using form post use the following code

A bucket can have multiple keys. A Key can store multiple objects.  In simple words Key is  folder name. Here we are using the Key name as UPLOADS.

using System;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

namespace AmazonAWS.Controllers
{
    public class S3Controller : Controller
    {
        private static readonly string _awsAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"];

        private static readonly string _awsSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"];

        private static readonly string _bucketName = ConfigurationManager.AppSettings["Bucketname"];

        public ActionResult UploadToS3(HttpPostedFileBase file)
        {
            try
            {
                IAmazonS3 client;
                using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(_awsAccessKey, _awsSecretKey))
                {
                    var request = new PutObjectRequest()
                    {
                        BucketName = _bucketName,
                        CannedACL = S3CannedACL.PublicRead,//PERMISSION TO FILE PUBLIC ACCESIBLE
                        Key =  string.Format("UPLOADS/{0}", file.FileName),
                        InputStream = file.InputStream//SEND THE FILE STREAM
                    };

                    client.PutObject(request);
                }
            }
            catch (Exception ex)
            {
                

            }
            return View();
        }
    }
}

Leave a Reply

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>