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,
Key = string.Format("UPLOADS/{0}", file.FileName),
InputStream = file.InputStream
};
client.PutObject(request);
}
}
catch (Exception ex)
{
}
return View();
}
}
}