views:

396

answers:

1

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

+2  A: 

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();
                }
            }
        }
    }
}
BigJoe714
Aha! Thanks! it looks so simple, but I will have a go with this and remember to click "answered" if it works. I will post my result.Regarding the "32Kb" byte array, why that size? is it a AWS standard or will more/less assigned to the buffer have impact on transfer speed or something?
BerggreenDK
Thats the buffer that they used in the AmazonS3_Sample located in the AWS SDK for .NET download http://aws.amazon.com/sdkfornet/
BigJoe714
I just got curious and downloaded the SDK again, and it seems my version was 1.5 which was a bit different, but still included a "Read object" function to. Now I've downloaded and installed the lastest and found the exact same sample as you've provided. Thanks for your time.Marking it as "THE" answer. :o)
BerggreenDK
Glad i could help. You should also thank Jonik for adding the amazon-web-services tag. I have an RSS feed for this tag, so I never saw it until he edited!
BigJoe714
Well sure, thanks to Jonik too. I will add the tag next time, but to me, I didnt see it as a web-service question, but more as a SDK for .net problem. :o) and in this case the real problem was an "Error 60"... but thanks!
BerggreenDK
Just tested it now. Works perfectly and smoothly. Thanks again!
BerggreenDK