Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / cloud

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

5.00/5 (2 votes)
9 Feb 2015CPOL1 min read 23.7K  
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.

What 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:

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.

C#
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();
        }
    }
}

The post Store files in Amazon S3 using AWS SDK in ASP.NET MVC appeared first on Venkat Baggu Blog.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)