简体   繁体   中英

download files from s3 bucket using C#

I am kind of new to AWS s3 bucket concept. I am suppose to download files from folder "TODAY FILE1" in s3 bucket and use it. I know how to do it in command line using command prompt. I do not know how to implement in C#.

Suppose

Here's what I do it command prompt

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

this is what I do it C# program and get error

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

I get an exception Amazon.Runtime.AmazonServiceException: 'Access to the path 'C:\\Temp' is denied

I do not know what to do Thanks MR

Create the files before writing:

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(@"C:\\Temp\\filename.xxx", bucketName, obj.Key);

Here is the another way of downloading files from S3 bucket. Suppose If you are working with Enterprise account with Enterprise client, you will not be able to get access key and secret key due to security concerns. In that scenario, you can use the below code snippet for downloading the file(any kind of format file).

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

}

If you need any help in this implementation, feel free to approach me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM