我想从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");
}
我得到一个访问被拒绝的异常,因为请求者支付访问设置。
TransferUtility
doesnot支持在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);