1

I have used the very basic code for downloading a file from Amazon S3. I have tried with two different codes.

  1. The one which is commented GetObjectResponse throwing error

    System.Xml.XmlException: There are multiple root elements. Line 2, position 2.
    at System.Xml.XmlTextReaderImpl.Throw(Exception e)
    at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
    at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res)
    at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
    at System.Xml.XmlTextReaderImpl.Read()
    at System.Xml.XmlTextReader.Read()
    

    and etc.,

  2. The code with the TransferUtilityDownloadRequest. Am not sure whether this method is correct or not. Found similar kind of example in Amazon Site so tried.

Source Code

  private static void AmazonS3Access()
    {
        string accessKey = "my_access_key";
        string secretKey = "my_secret_key";

        AmazonS3Config config = new AmazonS3Config();
        config.ServiceURL = "s3.amazonaws.com";

        AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                accessKey,
                secretKey,
                config);
        GetObjectRequest request = new GetObjectRequest();
        request.BucketName = "bucket";
        request.Key = "myfile.extension";
        try
        {
            TransferUtilityDownloadRequest myfile = new TransferUtilityDownloadRequest();                
            myfile.WithBucketName(request.BucketName);
            myfile.WithKey(request.Key);
            myfile.WithFilePath("D:\\S3File\\myfile.extension");  
            //GetObjectResponse response = client.GetObject(request);
            //response.WriteResponseStreamToFile("D:\\S3File\\myfile.extension");
        }
        catch (Exception Ex)
        {
            Console.WriteLine(Ex.ToString());
        }
    }

How to download the object from Amazon S3. Thanks in advance.

Note:

  1. Am using VS 2010 with .NetFramework 3.5
  2. Am using AmazonSDK.dll old version

Solution:

After adding the Network Proxy Credentials in the Program, it starts working fine.

Dineshkumar
  • 1,468
  • 4
  • 22
  • 49

2 Answers2

0

public Stream DownloadS3Object(string awsBucketName, string keyName)

    {
        using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client())
        {
            Stream imageStream = new MemoryStream();
            GetObjectRequest request = new GetObjectRequest { BucketName = awsBucketName, Key = keyName };
            using (GetObjectResponse response = client.GetObject(request))
            {
                response.ResponseStream.CopyTo(imageStream);
            }
            imageStream.Position = 0;
            // Clean up temporary file.
            // System.IO.File.Delete(dest);
            return imageStream;
        }
    }

pass the value in function get stream path save it in folder with use of below.

SaveStreamToFile(foldername + "/", MainStreamAwsPath);

and than you can apply simple c# code to download that folder.

Also Download latest AWS SDK for .net from aws.amazon.com/sdkfornet and add that dll in the project

Dipanki Jadav
  • 360
  • 3
  • 7
  • It is not getting the `GetObjectResponse` value properly.. `An unhandled exception of type 'System.Xml.XmlException' occurred in AWSSDK.dll Additional information: The 'link' start tag on line 5 position 2 does not match the end tag of 'head'. Line 6, position 3.` Error Message – Dineshkumar Dec 20 '13 at 11:03
  • Download latest sdk from http://aws.amazon.com/sdkfornet/ .and add dll in the project. – Dipanki Jadav Dec 20 '13 at 12:25
0
    public Stream DownloadS3Object(string awsBucketName, string keyName)

    {
        using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client())
        {
            Stream imageStream = new MemoryStream();
            GetObjectRequest request = new GetObjectRequest { BucketName = awsBucketName, Key = keyName };
            using (GetObjectResponse response = client.GetObject(request))
            {
                response.ResponseStream.CopyTo(imageStream);
            }
            imageStream.Position = 0;
            // Clean up temporary file.
            // System.IO.File.Delete(dest);
            return imageStream;
        }
    }

Note:

Am using VS 2010 with .NetFramework 3.5 Am using AmazonSDK.dll old version

Dipanki Jadav
  • 360
  • 3
  • 7