注意:回答我自己的问题以帮助其他人。
我正在关注official documentation从S3存储桶中获取文本文件并挂起:
static async Task ReadObjectDataAsync()
{
string responseBody = "";
try
{
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucketName,
Key = keyName
};
//THIS NEXT LINE HANGS!!!!
using (GetObjectResponse response = await client.GetObjectAsync(request))
using (Stream responseStream = response.ResponseStream)
using (StreamReader reader = new StreamReader(responseStream))
{
string title = response.Metadata["x-amz-meta-title"];
我如何让它工作?
这个问题在这里有解决方案https://github.com/aws/aws-sdk-net/issues/152
对我来说问题是我从WinForm应用程序运行此示例。
Winform apps Main()
方法标有Single Threaded Apartment属性[STAThread]
。这会导致Async失败。
要么删除[STAThread]
属性,要么在没有它的情况下制作另一个Main()
方法。