Upload files directly to Amazon S3 from ASP.NET application

后端 未结 5 1192
[愿得一人]
[愿得一人] 2020-11-29 20:57

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

相关标签:
5条回答
  • 2020-11-29 21:36

    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.

    0 讨论(0)
  • 2020-11-29 21:41

    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.

    0 讨论(0)
  • 2020-11-29 21:50

    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.

    0 讨论(0)
  • 2020-11-29 21:52

    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.

    0 讨论(0)
  • 2020-11-29 21:59

    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.

    0 讨论(0)
提交回复
热议问题