使用C#从Amazon S3存储桶下载对象时出错

时间:2013-12-11 07:14:22

标签: c# amazon-web-services amazon-s3

我使用了非常基本的代码从Amazon S3下载文件。 我试过两个不同的代码。

  1. 评论GetObjectResponse投掷错误

    的评论
    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()
    

    等,

  2. 包含TransferUtilityDownloadRequest的代码。我不确定这种方法是否正确。在Amazon Site中找到了类似的例子。

  3. 源代码

      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());
            }
        }
    

    如何从Amazon S3下载对象。提前谢谢。

    注意:

    1. 使用VS 2010和.NetFramework 3.5
    2. 使用AmazonSDK.dll旧版本
    3. 解决方案:

      在程序中添加网络代理凭据后,它开始正常工作。

2 个答案:

答案 0 :(得分: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;
        }
    }

传递函数获取流路径中的值将其保存在文件夹中,使用下面的内容。

SaveStreamToFile(foldername +“/”,MainStreamAwsPath);

并且您可以应用简单的c#代码来下载该文件夹。

还从aws.amazon.com/sdkfornet下载.net的最新AWS SDK并在项目中添加该dll

答案 1 :(得分: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;
        }
    }

注意:

使用VS 2010与.Net Framework 3.5 我使用的是Amazon SDK.dll旧版本