我正在使用AWS S3存储文本文件,然后使用EC2中的Windows服务下载它们。大约50%的时间我启动服务,从S3下载文件时服务挂起。这是我正在使用的代码:
GetObjectRequest request = new GetObjectRequest();
request.BucketName = BucketName;
request.Key = FilePathAndName;
using (GetObjectResponse response = objClient.GetObject(request))
{
string title = response.Metadata["x-amz-meta-title"];
using (Stream responseStream = response.ResponseStream)
{
using (StreamReader reader =
new StreamReader(responseStream))
{
responseBody = reader.ReadToEnd();
}
}
}
我也尝试过这段代码,而ReadAllResponseStream()是一种手动读取流的方法,而不是使用reader.ReadToEnd():
GetObjectRequest request = new GetObjectRequest();
request.BucketName = BucketName;
request.Key = FilePathAndName;
using (GetObjectResponse response = objClient.GetObject(request))
{
//string title = response.Metadata["x-amz-meta-title"];
//Console.WriteLine("The object's title is {0}", title);
using (Stream responseStream = response.ResponseStream)
{
byte[] s3Data = ReadAllResponseStream(responseStream);
responseBody = Encoding.UTF8.GetString(s3Data, 0, s3Data.Length);
}
}
如果我正在使用来自当地场所(EC2之外)的每个代码变体,它似乎工作正常。