如何使用.net的AWS SDK在请求者支付桶中下载S3对象?



我想从Amazon S3 Requester Pays桶中读取和下载数据。

我尝试在。net SDK中启用"请求者付费访问"选项,但我找不到任何内容:

static async Task DownloadFile()
{
TransferUtility fileTransferUtility =
new TransferUtility(new AmazonS3Client(accessKey, secretAccessKey, Amazon.RegionEndpoint.USEast1));
fileTransferUtility.Download("destination", "data-repository-client-1",
"20221002-22%2709%2753_to_20221002-22%2723%2715_Trades_v5.csv.gz");
}

我得到一个访问被拒绝的异常,因为请求者支付访问设置。

TransferUtilitydoesnot支持在Requester Pays bucket中下载对象

然而,当使用GetObject和自定义GetObjectRequest时,可能的。

您需要将RequestPayer参数设置为RequestPayer.Requester

这个应该可以工作:

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
var client = new AmazonS3Client(
accessKey,
secretAccessKey,
RegionEndpoint.USEast1
);
var request = new GetObjectRequest()
{
BucketName = "data-repository-client-1",
Key = "20221002-22%2709%2753_to_20221002-22%2723%2715_Trades_v5.csv.gz",
RequestPayer = RequestPayer.Requester
};
var filePath = "destination";
var appendToFile = false;
using (var response = await client.GetObjectAsync(request))
await response.WriteResponseStreamToFileAsync(filePath, appendToFile, CancellationToken.None);

相关内容

  • 没有找到相关文章

最新更新