c# - 使用 C# 从 s3 存储桶下载文件
问题描述
我对 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
不知道怎么办 谢谢MR
解决方案
在写入之前创建文件:
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);
}
}
推荐阅读
- javascript - 我的 shuffle 函数以单数形式工作,但是当将多个调用组合成一个函数时,它会中断
- scala - 当你可以使用可变类型的 val 时,为什么还要使用 var 呢?
- javascript - 如何从 Javascript 显示和生成 XML
- c# - 如何通过 API 传递三个参数并相应地检索信息
- r - 在R中提取具有同时出现的字母的单词
- c# - 如果托管在 IIS 上,ASP.NET 核心应用程序不会使用来自 RabbitMQ 队列的消息
- python - Python Pip“启动器中的致命错误”和更新 pip 的其他问题
- asp.net - 在 asp .net、IIS 8 环境中使用目录服务出现未知错误 (0x80005000)
- javascript - 在 ServiceNow 登录页面上找不到元素
- r - 在 `pkgdown` 解析参考部分的示例之前添加自定义 R 代码