【问题标题】:download files from s3 bucket using C#使用 C# 从 s3 存储桶下载文件
【发布时间】:2019-05-30 04:21:37
【问题描述】:

我对 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

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

【问题讨论】:

  • 您的机器上是否有一个名为C:\Temp 的文件夹?或者是否存在名为 C:\Temp 的文件?
  • 为什么要下载“C:\Temp”?您可以尝试使用您有权访问的真正现有文件吗?
  • 是的,我的本地机器上有一个文件夹 C:\temp
  • 运行您的代码的用户是否具有写入该文件的必要权限?是否以管理员身份运行?

标签: c# amazon-s3


【解决方案1】:

在写入之前创建文件:

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);
    }
}

【讨论】:

  • 使用fileTransferUtility.Download前无需创建文件。
【解决方案2】:

你必须把文件名:fileTransferUtility.Download(@"C:\Temp\filename.xxx", bucketName, obj.Key);

【讨论】:

  • 这是正确答案。要下载的文件的文件路径需要完整的文件名和扩展名。
【解决方案3】:

这是从 S3 存储桶下载文件的另一种方式。 假设如果您使用 Enterprise 帐户与 Enterprise 客户端一起工作,由于安全问题,您将无法获取访问密钥和密钥。 在这种情况下,您可以使用以下代码 sn-p 下载文件(任何格式的文件)。

protected void button1_click(object sender, EventArgs e)
{
try
{
string _FullName = lbFile.CommandName.ToString();
 string _FilePath = ConfigurationManager.AppSettings["SharedLocation"];
            string bucketName = ConfigurationManager.AppSettings["S3BucketName"];
//First I am creating a file with the file name in my local machine in a shared folder
            string FileLocation = _FilePath + "\\" + _FullName;
            FileStream fs = File.Create(_FullName);
            fs.Close();
            TransferUtility fileTransferUtility = new TransferUtility();
            fileTransferUtility.Download(FileLocation, bucketName, _FullName);
            fileTransferUtility.Dispose();
            WebClient webClient = new WebClient();
            HttpResponse response = HttpContext.Current.Response;
            response.Clear();
            response.ClearContent();
            response.ClearHeaders();
            response.Buffer = true;
            response.AddHeader("Content-Disposition", "attachment;filename=" + _FullName.ToString() + "");
            byte[] data = webClient.DownloadData(FileLocation);
            File.Delete(FileLocation); //After download starts, then I am deleting the file from the local path which I created initially.
            response.BinaryWrite(data);
            response.End();
}

}

如果您在此实施中需要任何帮助,请随时与我联系。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 2017-02-16
    • 2013-10-15
    • 2019-09-06
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2015-11-02
    相关资源
    最近更新 更多