28

My ASP.NET MVC application will take a lot of bandwidth and storage space. How can I setup an ASP.NET upload page so the file the user uploaded will go straight to Amazon S3 without using my web server's storage and bandwidth?

ycseattle
  • 3,687
  • 7
  • 36
  • 42

6 Answers6

27

Update Feb 2016:

The AWS SDK can handle a lot more of this now. Check out how to build the form, and how to build the signature. That should prevent you from needing the bandwidth on your end, assuming you need to do no processing of the content yourself before sending it to S3.

Jonathan Rupp
  • 15,522
  • 5
  • 45
  • 61
  • 1
    Nice!!! It's hard to find some of those Amazon samples and this was one I definitely could have used awhile ago. Thank you. – 410 Sep 28 '09 at 15:18
  • 5
    This is years later but I wrote a [blog](http://codeonaboat.wordpress.com/2011/04/22/uploading-a-file-to-amazon-s3-using-an-asp-net-mvc-application-directly-from-the-users-browser/) about this as I recently had to set it up. – floatingfrisbee Apr 25 '11 at 18:00
  • I don't understand this answer; you talk about the SDK being able to handle it, then give 2 links to documentation that doesn't mention the SDK at all. How about telling us which NuGet package(s) we need, which objects/methods to look at, etc.? – Jez Jun 11 '20 at 17:36
4

If you need to upload large files and display a progress bar you should consider the flajaxian component.

It uses flash to upload files directly to amazon s3, saving your bandwidth.

holiveira
  • 4,445
  • 9
  • 36
  • 39
  • 1
    Flajaxian was great, I used it in production for a couple of years. But recently it has become buggy on Mac OS due to subtle differences with the Flash component on Mac devices. I would no longer recommend this component for production use, the developers have abandoned support..you just need to look at all the unanswered posts on Codeplex. – QFDev Jun 23 '13 at 13:38
2

The best and the easiest way to upload files to amazon S3 via asp.net . Have a look at following blog post by me . i think this one will help. Here i have explained from adding a S3 bucket to creating the API Key, Installing Amazon SDK and writing code to upload files. Following are are the sample code for uploading files to amazon S3 with asp.net C#.

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
    }
    else
    {   // 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
}
}

Here you can use the function sendMyFileToS3 to upload file stream to amazon S3. For more details check my blog in the following link.

Upload File to Amazon S3 via asp.net

I hope the above mentioned link will help.

sambit.albus
  • 352
  • 1
  • 7
  • 26
  • This code referer's to paid solution and does it directly upload to Amazon s3 bucket?. – Learning Feb 28 '16 at 04:52
  • Yes , it directly upload to amazon S3 bucket specified by you. You just need to put your API credentials and bucket name where you want to upload the files. Rest will be take care by the code. It is described in details in the blog post. – sambit.albus Feb 28 '16 at 10:37
  • 1
    I think the blog link is broken (or site is experiencing issues). Can you please check and update. Thanks! – Prashant Agarwal Apr 21 '18 at 08:36
1

Look for a javascript library to handle the client side upload of these files. I stumbled upon a javascript and php example Dojo also seems to offer a clientside s3 file upload.

Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
0

ThreeSharp is a library to facilitate interactions with Amazon S3 in a .NET environment.

You'll still need to host the logic to upload and send files to s3 in your mvc app, but you won't need to persist them on your server.

Kyle Chafin
  • 607
  • 8
  • 16
  • 5
    ThreeSharp is stoping thier project due to Amazon's efforts. This is from thier site: "We recommend developers migrate to the AWS SDK at their earliest convenience, as we will no longer be maintaining the ThreeSharp library. " – Stradas Dec 15 '09 at 14:59
  • @kyle, Does this mean AWS SDK has build in feature to stream files directly to AWS S3 bucket... – Learning Feb 28 '16 at 04:56
0

Save and GET data in aws s3 bucket in asp.net mvc :-

To save plain text data at amazon s3 bucket.

1.First you need a bucket created on aws than

2.You need your aws credentials like a)aws key b) aws secretkey c) region

// code to save data at aws // Note you can get access denied error. to remove this please check AWS account and give //read and write rights

Name space need to add from NuGet package

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

 var credentials = new Amazon.Runtime.BasicAWSCredentials(awsKey, awsSecretKey);          
                try`
                {
                    AmazonS3Client client = new AmazonS3Client(credentials, RegionEndpoint.APSouth1);
                    // simple object put
                    PutObjectRequest request = new PutObjectRequest()
                    {
                        ContentBody = "put your plain text here",
                        ContentType = "text/plain",
                        BucketName = "put your bucket name here",
                        Key = "1"
                        //put unique key to uniquly idenitify your data
                        // you can pass here any data with unique id like primary key 
                        //in db
                    };              
                    PutObjectResponse response = client.PutObject(request);
                   
                }
                catch(exception ex)
                {
                     //
                }

Now go to your AWS account and check the bucket you can get data with "1" Name in the AWS s3 bucket. Note:- if you get any other issue please ask me a question here will try to resolve it.

To get data from AWS s3 bucket:-

 try
            {              
                var credentials = new Amazon.Runtime.BasicAWSCredentials(awsKey, awsSecretKey);
                AmazonS3Client client = new AmazonS3Client(credentials, RegionEndpoint.APSouth1);
                GetObjectRequest request = new GetObjectRequest()
                {
                    BucketName = bucketName,
                    Key = "1"// because we pass 1 as unique key while save 
                            //data at the s3 bucket
                };

                using (GetObjectResponse response = client.GetObject(request))
                {
                    StreamReader reader = new 
                 StreamReader(response.ResponseStream);
                    vccEncryptedData = reader.ReadToEnd();
                }
            }
            catch (AmazonS3Exception)
            {
                throw;
            }