C# 使用C将文件添加到AmazonS3上的bucket中#

C# 使用C将文件添加到AmazonS3上的bucket中#,c#,.net,amazon-s3,C#,.net,Amazon S3,如何将位图对象作为图像保存到AmazonS3 我已经准备好了一切,但我有限的C夏普阻止了我完成这项工作 // I have a bitmap iamge Bitmap image = new Bitmap(width, height); // Rather than this image.save(file_path); // I'd like to use S3 S3 test = new S3(); test.WritingAnObject("images", "testing2.png

如何将位图对象作为图像保存到AmazonS3

我已经准备好了一切,但我有限的C夏普阻止了我完成这项工作

// I have a bitmap iamge
Bitmap image = new Bitmap(width, height);

// Rather than this
image.save(file_path);

// I'd like to use S3
S3 test = new S3();
test.WritingAnObject("images", "testing2.png", image);

// Here is the relevant part of write to S3 function
PutObjectRequest titledRequest = new PutObjectRequest();
titledRequest.WithMetaData("title", "the title")
             .WithContentBody("this object has a title")
             .WithBucketName(bucketName)
             .WithKey(keyName);
如您所见,S3函数只能接收字符串并将其保存为文件体

我该如何写这篇文章,使它允许我传入位图对象并将其保存为图像?也许是一条小溪?还是作为字节数组


非常感谢您的帮助。

设置请求对象的InputStream属性:

titledRequest.InputStream = image;

您可以将
用于InputStream
用于FilePath
。例如,在将新图像保存到S3时:

using (var memoryStream = new MemoryStream())
{
    using(var yourBitmap = new Bitmap())
    {
        //Do whatever with bitmap here.
        yourBitmap.Save(memoryStream, ImageFormat.Jpeg); //Save it as a JPEG to memory stream. Change the ImageFormat if you want to save it as something else, such as PNG.
        PutObjectRequest titledRequest = new PutObjectRequest();
        titledRequest.WithMetaData("title", "the title")
            .WithInputStream(memoryStream) //Add file here.
            .WithBucketName(bucketName)
            .WithKey(keyName);
    }
}

嗯。。。将图像保存到文件,将文件转换为十六进制字符串并传递该字符串?我对s3一无所知,但我认为这会有所帮助。我不想先把图像保存到文件中。我确信这可以在图像仍在内存中时完成。然后将其保存到内存流。好的,这很有意义,我如何将位图对象转换为内存流?如果这是一个简单的问题,我深表歉意,但对于c#,我是一个新手。@Abs:我更新了代码示例,添加了一个将位图保存到MemoryStream的示例。它成功了。但是我不得不在
WithKey(keyName)
之后调用
.WithInputStream(memoryStream)
。现在,我只需要解决一个问题:当我尝试执行此操作时,无法访问封闭流:
使用(S3Response-responseWithMetadata=client.PutObject(titledRequest))
-有什么想法吗?@vcsjones-它位于我粘贴在上述注释中的行上。这不是你的代码,只是我认为我可以得到响应的东西。@Abs:your
client.PutObject(titledRequest)调用应该在using语句中(在第16行和第17行之间)。