AWS S3 GetObjectAsync挂起/超时

时间:2018-06-19 04:15:19

标签: c# amazon-web-services amazon-s3 async-await hang

注意:回答我自己的问题,以便将来帮助其他人。

我跟随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"];

如何让它发挥作用?

1 个答案:

答案 0 :(得分:1)

此问题的解决方案https://github.com/aws/aws-sdk-net/issues/152

问题在于我从WinForm应用程序运行此示例。

Winform apps Main()方法标有Single Threaded Apartment属性[STAThread]。这会导致Async失败。

删除[STAThread]属性或不使用其他Main()方法。