开发者

Using MVC to talk directly to Amazon S3

开发者 https://www.devze.com 2023-03-14 19:24 出处:网络
I am writing an MVC 3 application that needs to allow the user to directly upload a file to S3.I also need to show a progress bar.All of the examples I have seen are PHP or Ruby-on-Rails related.Has a

I am writing an MVC 3 application that needs to allow the user to directly upload a file to S3. I also need to show a progress bar. All of the examples I have seen are PHP or Ruby-on-Rails related. Has anyone managed to uploa开发者_JS百科d a file to S3 directly (from client browser) using MVC?


So, after a morning of smashing my head into my keyboard, the following snippet of code works (with the obvious credentials removed):

using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client("Access_Key", 
                                                                      "Secret_Key"))
{
    PutObjectRequest request = new PutObjectRequest();
    request.WithBucketName("BUCKET-NAME")
           .WithCannedACL(S3CannedACL.PublicRead)
           .WithKey("myDirectory/" + 
                    HttpContext.Current.Server.UrlEncode(fileBase.FileName))
           .InputStream = fileBase.InputStream;
    S3Response response = client.PutObject(request);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消