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

时间:2019-01-03 14:50:35

标签: c# amazon-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:'对路径'C:\ Temp'的访问被拒绝

我不知道该怎么办 谢谢

3 个答案:

答案 0 :(得分: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 Excep)
     {
         Console.WriteLine(Excep.Message, Excep.InnerException);
    }
}

答案 1 :(得分:1)

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

答案 2 :(得分:0)

这是从S3存储桶下载文件的另一种方法。 假设如果您使用带有Enterprise Client的Enterprise帐户,则出于安全考虑,您将无法获取访问密钥和秘密密钥。 在这种情况下,您可以使用以下代码段下载文件(任何格式的文件)。

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

}

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