I have used the very basic code for downloading a file from Amazon S3. I have tried with two different codes.
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.,
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:
Solution:
After adding the Network Proxy Credentials in the Program, it starts working fine.
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.