有没有办法使用适用于 .NET 的 AWS 开发工具包将对象从 S3 存储桶下载到文件目标?



我正在尝试找到一种使用 AWS 开发工具包 .NET 将对象从 S3 存储桶下载到文件目标的方法。

我可以获取对象列表并删除对象,但我看不到您可以下载对象的任何地方。请帮忙。

获取对象时,S3 C# API 具有用于将响应流直接保存到文件的帮助程序方法。如果需要对文件进行更多控制,还可以打开文件流并将响应流手动写入文件。

https://docs.aws.amazon.com/sdkfornet1/latest/apidocs/html/T_Amazon_S3_Model_GetObjectResponse.htm

var client = new AmazonS3Client("yourAccessId", "yourSecretKey");

using (var obj = client.GetObject("yourBucket", "yourObject"))
{
obj.WriteResponseStreamToFile("some/file/path.ext");
}

您还可以使用S3 传输实用程序直接下载文件,而无需使用流对象。例如:

using Amazon.S3;
using Amazon.S3.Transfer;
// Replace the following variables with the appropriate
// values for the files for the files you want to download.
var bucketName = "s3photos";
var s3Key = "mypicture.jpg";
var filePath = "downloadpath" + "\" + s3Key;
var client = new AmazonS3Client();
var transferUtil = new TransferUtility(client);
await transferUtility.DownloadAsync(new TransferUtilityDownloadRequest
{
BucketName = bucketName,
Key = s3Key,
FilePath = filePath,
});
// Check to see if the file was downloaded.
if (File.Exists(filePath))
{
Console.WriteLine("File successfully downloaded.");
}
else
{
Console.WriteLine($"File could not be downloaded. Make sure {s3Key}");
Console.WriteLine($"exists in the bucket, {bucketName}.");
}

最新更新