ASP.NET MVC - Uploading an image to Amazon S3

前端 未结 4 1419
甜味超标
甜味超标 2020-12-30 08:03

I have my image from Request.Files[0]. Now, how do I upload this image to S3? I see that in the AWS .NET API you have to specify ContentBody when putting an object which is

4条回答
  •  感情败类
    2020-12-30 08:29

    Most likely this is a Base64-encoded string, but you should check the S3 documentation to be sure. If it is, you should use Convert.ToBase64String() and pass it the byte array.

    Here's some sample code you can try. I haven't tested it, but it should help you get the right idea:

    if (Request.Files.Count >= 1) {
        var file = Request.Files[0];
        var fileContents = new byte[file.ContentLength];
        file.InputStream.Read(fileContents, 0, file.ContentLength);
        var fileBase64String = Convert.ToBase64String(fileContents);
    
        // now you can send fileBase64String to the S3 uploader
    }
    

提交回复
热议问题