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

15,732

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();
                }
            }
        }
    }
}
Share:
15,732
BerggreenDK
Author by

BerggreenDK

Started programming as very young on a Commodore VIC-20 and 64. First in BASIC, then moved to 8-bit assembler/machinecode to make things run more smooth. Ended up building a game for it with two friends which we sold and earned Amiga 500 computers. This lead me to the assembler of Motorola 680x0 series CPU. Great CPU btw. Took an IT-education, learned C/C++ and used some Pascal. Build my first networking application for the school I was on, as we needed a cross location chat-tool in DOS. Made it upon Netware/IPX protocol. Then I went and became in love with C++ and its possiblities. Got brainwashed at school, came out saying: Windows, Windows, Windows... so I started to look at DirectX for direct computer power, usable for further multimedia/game development. I've composed a some computer-music too, back in the old C64 days, but thats another story. Later on, I got employed in different companies, including IT-consulting and support. Then worked with advertising/commercial companies as "Multimedia architect/systemdeveloper". I've been all the way through the different browserplatforms and "wars", been building cross platform websites with dynamic HTML from the very early days with Netscape LAYER tags and MSIE DIV, makeing sure that things ALSO worked on Macintosh platoform, so cross platform development has been my goal since 1997. I design and implement SQL-server structures too, so the backend is ready for the frontend. Today I run my own small company, working mostly with .NET and C#, but XHTML/CSS/XML/JQUERY + some Flash Actionscript 3.0 is also part of my work from time to time. Overall - the company is focusing on making the internet easier to learn and use, therefore somethings might be hard to develop, but as long as it will help the user to get respons/results faster and easier, we still go for the "hard way" of developing stuff. Better think before errors happends. I dont have a "language" religion, I dont care what platform we use, as long as its the best for the purpose and we dont have to pay gazillions for licenses. I dont mind paying for quality, but dang - there is really a lot of wanna-be crap out there, so I do check upon things before investing time and money. And no, I dont go for the easy way. I go for the most valueable way, for the customer.

Updated on August 05, 2022

Comments

  • BerggreenDK
    BerggreenDK almost 2 years

    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???