gpt4 book ai didi

c# - 使用 C# 从 s3 存储桶下载文件

转载 作者:行者123 更新时间:2023-12-04 11:43:03 25 4
gpt4 key购买 nike

我对 AWS s3 存储桶概念有点陌生。我想从 s3 存储桶中的文件夹“TODAY FILE1”下载文件并使用它。我知道如何使用命令提示符在命令行中执行此操作。我不知道如何在 C# 中实现。

认为

这是我在命令提示符下执行的操作

C:\> aws s3 cp "s3://mys3bucket-output/TODAY FILE1" . --recursive

这就是我做的 C# 程序并得到错误
string accessKey = "abc123";
string secretKey = "secret123";
string bucketName = "mys3bucket-output"
TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.USEast2));


BasicAWSCredentials basicCredentials = new BasicAWSCredentials(accessKey,secretKey);
AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey), Amazon.RegionEndpoint.USEast2);
ListObjectsRequest request = new ListObjectsRequest();

ListObjectsResponse response = s3Client.ListObjects(request.BucketName= bucketName, request.Prefix="TODAY FILE1/");

foreach (S3Object obj in response.S3Objects)
{
try
{
Console.WriteLine("{0}", obj.Key);
fileTransferUtility.Download(@"C:\Temp", bucketName, obj.Key);

}
catch (Exception Excep)
{
Console.WriteLine(Excep.Message, Excep.InnerException);
}
}

我收到异常 Amazon.Runtime.AmazonServiceException: 'Access to the path 'C:\Temp' is denied

我不知道该怎么办
谢谢先生

最佳答案

在写入之前创建文件:

foreach (S3Object obj in response.S3Objects)
{
try
{
string filename = directoryPath + "\\" + obj.Key;
FileStream fs = File.Create(filename);
fs.Close();
Console.WriteLine("{0}", obj.Key);
fileTransferUtility.Download(filename, bucketName, obj.Key);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, ex.InnerException);
}
}

关于c# - 使用 C# 从 s3 存储桶下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54024645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com