You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWSSDK.S3 GetObjectAsync调用挂起问题排查求助

AmazonS3Client方法调试时挂起问题分析与解决

问题现象

  • 初始将s3Client作为私有字段时,调用GetFileMetadataAsync出现挂起;尝试每次创建新客户端后问题仍存在
  • 调用GetObjectAsync流程中:第一次请求正常返回404错误;进入catch块发起第二次请求时,Fiddler显示服务端返回403,但GetObjectAsync方法挂起未释放控制权,线程窗口显示线程处于等待Semaphore状态
  • 仅在调试模式下按F10单步执行时触发挂起,无断点直接运行程序时结果可正常返回

环境信息

  • AWSSDK.S3版本:4.0.19.4
  • .NET版本:10
  • 应用类型:控制台程序,目标方法在System.Timers.Timer的Tick事件中执行
  • IDE差异:问题仅出现在Visual Studio 2026 v18.4.1;切换至Visual Studio 2022测试,代码运行完全正常

相关代码

客户端配置代码

_awsConfig = new AmazonS3Config
{
    ServiceURL = serviceUrl.TrimEnd('/'),
    ForcePathStyle = true, 
    UseHttp = serviceUrl.StartsWith("http://"),
    MaxErrorRetry = _maxRetries,
    RetryMode = RequestRetryMode.Standard,
    AuthenticationRegion = ""
};

_awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

业务逻辑代码

public async Task<Stream> DownloadFileAsync(string objectKey)
{
    try
    {
        using var s3Client = new AmazonS3Client(_awsCredentials, _awsConfig);
        var request = new GetObjectRequest
        {
            BucketName = _bucketName,
            Key = objectKey
        };

        using var response = await s3Client.GetObjectAsync(request);
        return response.ResponseStream;
    }
    catch (AmazonS3Exception ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
    {
        if (objectKey.Contains("/"))
        {
            var fileName = objectKey.Substring(objectKey.IndexOf('/'));
            var fallbackRequest = new GetObjectRequest
            {
                BucketName = _bucketName,
                Key = fileName
            };
            using var s3Client = new AmazonS3Client(_awsCredentials, _awsConfig);
            var response = await s3Client.GetObjectAsync(fallbackRequest);
            return response.ResponseStream;
        }

        throw new Exception($"FileNotFound: {objectKey}", ex);
    }
    catch (AmazonS3Exception ex)
    {
        throw new Exception($"Error downloading file: {ex.Message}", ex);
    }
}

结论

该问题并非代码或AWS客户端配置错误,而是Visual Studio 2026 v18.4.1的调试功能bug——调试过程中抛出异常会触发程序挂起。切换至Visual Studio 2022运行相同代码,无任何挂起现象。

内容的提问来源于stack exchange,提问作者Sergey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.01 12:22:30