gpt4 book ai didi

c# - 使用 C# 将文件添加到 Amazon S3 上的存储桶

转载 作者:太空狗 更新时间:2023-10-29 20:57:20 27 4
gpt4 key购买 nike

如何将位图对象作为图像保存到 Amazon S3?

我已经设置好一切,但我有限的 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 函数只能接收一个字符串并将其保存为文件的主体。

我怎样才能以允许我传入位图对象并将其保存为图像的方式编写它?也许作为一个流?还是字节数组?

感谢任何帮助。

最佳答案

您将使用 WithInputStreamWithFilePath。例如将新图像保存到 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);
}
}

关于c# - 使用 C# 将文件添加到 Amazon S3 上的存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6477980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com