如何从AWS S3 Storage下载文件作为字节数组?

时间:2018-09-21 07:16:49

标签: c# windows amazon-web-services amazon-s3

我可以使用以下代码从Amazon S3存储下载文件

var s3Client = new AmazonS3Client(txt_Accesskey.Text, txt_Secretkey.Text, bucketRegion);
GetObjectRequest request = new GetObjectRequest();
request.BucketName = bucketName;
request.Key = "Sample.txt";
GetObjectResponse response = s3Client.GetObject(request);
response.WriteResponseStreamToFile(@"C:\Desktop\Sample.txt");

但是,我想将这些文件下载为字节数组。有人可以给我解决方案吗?

1 个答案:

答案 0 :(得分:1)

只要快速查看docs,您也许就能做到

using (GetObjectResponse response = client.GetObject(request))
   using (var ms = new MemoryStream(response.ResponseStream))
       bytes = ms.ToArray();