Click here to Skip to main content
15,895,799 members
Articles / Web Development / ASP.NET

Upload Base64 Data to S3 using AWS SDK in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
9 Feb 2015CPOL 17K   4   1
How to save or upload Base64 data to S3 using AWS SDK in ASP.NET MVC

In this post, we will learn how to save or upload Base64 data to S3 using AWS SDK in ASP.NET MVC. You can check the previous post here on how to store files in Amazon S3 using AWS SDK in ASP.NET MVC.

Implementation

First, you need to convert the Base64 string to Byte Array and then we convert that into Stream object and send that request to S3.

Use the following snippet of code to save Base64 or Byte Array data S3.

C#
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(string base64String)
{
    try
    {
        IAmazonS3 client;
        byte[] bytes = Convert.FromBase64String(base64String);

        using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(_awsAccessKey, _awsSecretKey))
        {
            var request = new PutObjectRequest
            {
                BucketName = _bucketName,
                CannedACL = S3CannedACL.PublicRead,
                Key = string.Format("UPLOADS/{0}", file.FileName)
            };
            using (var ms = new MemoryStream(bytes))
            {
                request.InputStream = ms;
                client.PutObject(request);
            }
        }
    }
    catch (Exception ex)
    {
        

    }
    return View();
}

The post Upload Base64 data to 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)


Written By
Software Developer (Senior) eBiz Solutions http://venkatbaggu.com/
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHave you worked with Lambdy templates? Pin
Alireza Ali11-Jun-21 10:43
Alireza Ali11-Jun-21 10:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.