使用C#将文件添加到Amazon S3上的存储桶
发布时间:2020-12-15 18:21:39 所属栏目:百科 来源:网络整理
导读:如何将位图对象作为图像保存到Amazon S3? 我已经完成了所有设置但是我有限的C sharp阻止了我完成这一切. // I have a bitmap iamgeBitmap image = new Bitmap(width,height);// Rather than thisimage.save(file_path);// I'd like to use S3S3 test = new
如何将位图对象作为图像保存到Amazon S3?
我已经完成了所有设置但是我有限的C sharp阻止了我完成这一切. // 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函数只能接受一个字符串并将其保存为文件正文. 我怎样才能以这样的方式编写它,它允许我传入一个位图对象并将其保存为图像?也许作为一个流?还是作为字节数组? 我感谢任何帮助. 解决方法
您将使用WithInputStream或WithFilePath.例如,在将新图像保存到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); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |