ASP.NET MVC - 将图像上传到 Amazon S3

发布于 2024-09-24 20:04:53 字数 107 浏览 0 评论 0原文

我的图像来自 Request.Files[0]。现在,我如何将此图像上传到 S3?我发现在 AWS .NET API 中,在放置字符串对象时必须指定 ContentBody。我如何获取文件的内容主体?

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 a string. How would I get the content body of my file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纸短情长 2024-10-01 20:04:53
var file = Request.Files[0];
PutObjectRequest request = new PutObjectRequest();
request.BucketName = "mybucket"
request.ContentType = contentType;
request.Key = key;
request.InputStream = file.InputStream;
s3Client.PutObject(request);
var file = Request.Files[0];
PutObjectRequest request = new PutObjectRequest();
request.BucketName = "mybucket"
request.ContentType = contentType;
request.Key = key;
request.InputStream = file.InputStream;
s3Client.PutObject(request);
醉酒的小男人 2024-10-01 20:04:53

有关如何使用文件夹以及授予所有用户只读访问权限的更详细信息。
网页:

C#

HttpPostedFileBase file = Request.Files[0];
   if (file.ContentLength > 0) // accept the file
        {
            string accessKey = "XXXXXXXXXXX";
            string secretKey = "122334XXXXXXXXXX";
            AmazonS3 client;
            using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
            {
                MemoryStream ms = new MemoryStream();
                PutObjectRequest request = new PutObjectRequest();
      request.WithBucketName("mybucket")
     .WithCannedACL(S3CannedACL.PublicRead)
     .WithKey("testfolder/test.jpg").InputStream = file.InputStream;
       S3Response response = client.PutObject(request);
            }

更多详细信息请参见:http: //bradoyler.com/post/3614362044/uploading-an-image-with-aws-sdk-for-net-c

Slightly more detail with how to use folders and to grant all users read-only access.
Html:

C#

HttpPostedFileBase file = Request.Files[0];
   if (file.ContentLength > 0) // accept the file
        {
            string accessKey = "XXXXXXXXXXX";
            string secretKey = "122334XXXXXXXXXX";
            AmazonS3 client;
            using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
            {
                MemoryStream ms = new MemoryStream();
                PutObjectRequest request = new PutObjectRequest();
      request.WithBucketName("mybucket")
     .WithCannedACL(S3CannedACL.PublicRead)
     .WithKey("testfolder/test.jpg").InputStream = file.InputStream;
       S3Response response = client.PutObject(request);
            }

More detail is available here: http://bradoyler.com/post/3614362044/uploading-an-image-with-aws-sdk-for-net-c

半葬歌 2024-10-01 20:04:53

这很可能是 Base64 编码的字符串,但您应该检查 S3 文档来确定。如果是,您应该使用 Convert.ToBase64String() 并传递它字节数组。

这里有一些您可以尝试的示例代码。我还没有测试过它,但它应该可以帮助你得到正确的想法:

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
}

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
}
在风中等你 2024-10-01 20:04:53
                PurObjectRequest request = new PutObjectRequest()
                {
                    BucketName = _bucketName,
                    CannedACL = S3CannedACL.PublicRead,
                    Key =  string.Format("folderyouwanttoplacethefile/{0}", file.FileName),
                    InputStream = file.InputStream
                };

                YourS3client.PutObject(request);
                PurObjectRequest request = new PutObjectRequest()
                {
                    BucketName = _bucketName,
                    CannedACL = S3CannedACL.PublicRead,
                    Key =  string.Format("folderyouwanttoplacethefile/{0}", file.FileName),
                    InputStream = file.InputStream
                };

                YourS3client.PutObject(request);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文