Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve binary data from S3 storage through AWS.NET in C#

I've tested most of the included samples in the AWS SDK for .NET and they all works fine.

I can PUT objects, LIST objects and DELETE objects in a bucket, but... lets say I delete the original and want to sync those files missing locally?

I would like to make a GET object (by key/name and bucket ofcause). I can find the object, but how do I read the binary data from S3 through the API?

Do I have to write my own SOAP wrapper for this or is there some kinda sample for this out "here" ? :o)

In hope of a sample. It does not have to tollerate execeptions etc. I just need to see the main parts that connects, retreives and stores the file back on my ASP.net or C# project.

Anyone???

like image 556
BerggreenDK Avatar asked Mar 16 '10 15:03

BerggreenDK


People also ask

How do I pull data from AWS S3 bucket?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.

What is S3 binary?

Cloud CMS supports Amazon S3 (Simple Storage Service) by allowing it to serve as a backend provider for the storage of binary files within Cloud CMS. When files are uploaded to Cloud CMS, they are parsed and worked with and then eventually sent over to Amazon S3 for persistence.

How do I view contents of S3 bucket?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object. In the Objects list, choose the name of the object for which you want an overview. The object overview opens.

How do I get S3 objects?

You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.


1 Answers

Here is an example:

string bucketName = "bucket";
string key = "some/key/name.bin";
string dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "name.bin");

using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKeyID, AWSSecretAccessKeyID))
{
    GetObjectRequest getObjectRequest = new GetObjectRequest().WithBucketName(bucketName).WithKey(key);

    using (S3Response getObjectResponse = client.GetObject(getObjectRequest))
    {
        if (!File.Exists(dest))
        {
            using (Stream s = getObjectResponse.ResponseStream)
            {
                using (FileStream fs = new FileStream(dest, FileMode.Create, FileAccess.Write))
                {
                    byte[] data = new byte[32768];
                    int bytesRead = 0;
                    do
                    {
                        bytesRead = s.Read(data, 0, data.Length);
                        fs.Write(data, 0, bytesRead);
                    }
                    while (bytesRead > 0);
                    fs.Flush();
                }
            }
        }
    }
}
like image 76
BigJoe714 Avatar answered Sep 20 '22 18:09

BigJoe714