我在 S3 存储桶中有几个数据(csv)文件排列在以下虚拟目录中:
s3://mybucketname/data/Type1/
存储桶名称:mybucketname
密钥前缀:/data/Type1/
我想在这个bucket+key中组合这些csv文件的内容并将输出作为字符串返回。代码 -
public string GetBucketContents(string bucketName)
{
string bucketContent = "";
ListObjectsRequest request = new ListObjectsRequest
{
BucketName = bucketName,
Prefix = "data/Type1/"
};
using (ListObjectsResponse response = amazonS3Client.ListObjects(request))
{
//Loop all objects in S3 bucket
foreach (S3Object s3Object in response.S3Objects)
{
//Get S3 object @ s3Object.Key
GetObjectRequest objectRequest = new GetObjectRequest().WithBucketName(bucketName).WithKey(s3Object.Key);
using (GetObjectResponse objectResponse = amazonS3Client.GetObject(objectRequest))
{
using (StreamReader reader = new StreamReader(objectResponse.ResponseStream))
{
bucketContent += reader.ReadToEnd();
}
}
}
}
return bucketContent;
}
我得到了预期的输出:-)
但仍然想验证是否有更好的(性能方面)方法来实现这一目标?
谢谢!