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

7

我是一个新手,对于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' 被拒绝'

我不知道该怎么办 谢谢 MR


2
你的电脑上有一个名为 C:\Temp 的文件夹吗?或者存在一个名为 C:\Temp 的文件吗? - mason
为什么要下载“C:\Temp”文件夹? 您能否尝试使用您可以访问的真实存在的文件? - devdimi
是的,我在本地机器上有一个“C:\temp”文件夹。 - user2726975
1
你的代码运行所使用的用户,拥有写入该文件的必要权限了吗?它是否以管理员身份运行? - mason
3个回答

6

在编写代码之前先创建文件:

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之前不需要创建文件。 - Nandostyle

3

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


这是正确的答案。要下载的文件路径需要包括完整的文件名和扩展名。 - Nandostyle

0

这里是从S3存储桶下载文件的另一种方法。 假设您正在与企业客户合作的企业帐户中工作,由于安全问题,您将无法获得访问密钥和秘密密钥。 在这种情况下,您可以使用以下代码片段下载文件(任何格式的文件)。

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

}

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接