c# - AWS S3 GetObjectAsync 挂起/超时

标签 c# amazon-web-services amazon-s3 async-await freeze

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

我正在关注 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 应用程序 Main() 方法标有单线程单元属性 [STAThread]。这会导致异步失败。

要么删除 [STAThread] 属性,要么创建另一个没有它的 Main() 方法。

关于c# - AWS S3 GetObjectAsync 挂起/超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50920575/

相关文章:

c# - 为什么委托(delegate)类型派生自 MulticastDelegate 类为什么不直接派生自 Delegate 类?

.net - Amazon SES - 电子邮件地址中的非 ASCII 字符

amazon-web-services - 使用 tar.gz 文件作为 Amazon Athena 的源

c# - 从类(class)获取列表时出错

c# - C++ 与 C# 类型安全

postgresql - 增加 RDS 中的预配置 IOPS 会导致停机?

amazon-web-services - 如何在 Ubuntu 16.04 上安装 AWS SAM Local?

hadoop - EMRFS 是否使 S3 对外部客户端保持一致

amazon-web-services - CloudFormation 嵌套模板能否在没有预签名 URL 的情况下访问私有(private) S3 对象?

c# - 如何缩小字符串并在以后找到原始字符串