Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download files from s3 bucket using C#

Tags:

c#

amazon-s3

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

like image 510
user2726975 Avatar asked Jan 03 '19 14:01

user2726975


People also ask

How do I download data from S3 bucket?

To download an entire bucket to your local file system, use the AWS CLI sync command, passing it the s3 bucket as a source and a directory on your file system as a destination, e.g. aws s3 sync s3://YOUR_BUCKET . . The sync command recursively copies the contents of the source to the destination.

How do I download from S3 bucket to local using command line?

You can use cp to copy the files from an s3 bucket to your local system. Use the following command: $ aws s3 cp s3://bucket/folder/file.txt . To know more about AWS S3 and its features in detail check this out!

How do I download multiple files from S3 bucket to local?

Unfortunately AWS S3 Console doesn't have an option to download all the content of an S3 bucket at the moment, but there are other options like AWS CLI to do so. For instance: aws s3 sync s3://all_my_stuff_bucket . This command will start downloading all the objects in all_my_stuff_bucket to the current directory.

Can I download multiple files from S3?

If you have Visual Studio with the AWS Explorer extension installed, you can also browse to Amazon S3 (step 1), select your bucket (step 2), select al the files you want to download (step 3) and right click to download them all (step 4).


1 Answers

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);
    }
}
like image 161
Denis Wang Avatar answered Sep 30 '22 18:09

Denis Wang