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

如何使用AWS SDK for .NET下载Requester Pays存储桶中的S3对象

解决Amazon S3 Requester Pays存储桶的.NET SDK访问问题

Requester Pays存储桶要求请求方承担访问产生的费用,.NET SDK默认不会自动启用该选项,这是导致你访问拒绝的核心原因。以下是两种可行的解决方式:

方法一:使用TransferUtilityDownloadRequest配置请求方付费

通过TransferUtility的重载方法,显式设置RequesterPays属性为true:

static async Task DownloadFile()
{
    var s3Client = new AmazonS3Client(accessKey, secretAccessKey, Amazon.RegionEndpoint.USEast1);
    var transferUtility = new TransferUtility(s3Client);
    
    var downloadRequest = new TransferUtilityDownloadRequest
    {
        BucketName = "data-repository-client-1",
        Key = "20221002-22%2709%2753_to_20221002-22%2723%2715_Trades_v5.csv.gz",
        FilePath = "destination",
        RequesterPays = true // 启用请求方付费标识
    };
    
    await transferUtility.DownloadAsync(downloadRequest);
}

方法二:直接通过GetObjectRequest设置

如果不需要使用TransferUtility,可以直接构造GetObjectRequest并开启请求方付费:

static async Task DownloadFileDirectly()
{
    var s3Client = new AmazonS3Client(accessKey, secretAccessKey, Amazon.RegionEndpoint.USEast1);
    
    var getRequest = new GetObjectRequest
    {
        BucketName = "data-repository-client-1",
        Key = "20221002-22%2709%2753_to_20221002-22%2723%2715_Trades_v5.csv.gz",
        RequesterPays = true
    };
    
    using (var response = await s3Client.GetObjectAsync(getRequest))
    {
        await response.WriteResponseStreamToFileAsync("destination", false, CancellationToken.None);
    }
}

注意事项

  • 确保你的AWS账号有足够的余额承担该桶的请求和数据传输费用
  • 用于访问的IAM用户需具备s3:GetObject权限,以及相关的费用结算权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 07:31:06