使用C#从Amazon S3存储桶下载对象时出错 [英] Error downloading an object from Amazon S3 bucket using C#

查看:98
本文介绍了使用C#从Amazon S3存储桶下载对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了非常基本的代码从Amazon S3下载文件. 我尝试了两种不同的代码.

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

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

等,

带有TransferUtilityDownloadRequest的代码.不确定此方法是否正确.在 Amazon网站尝试过.

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.

源代码

  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下载对象.预先感谢.

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

注意:

  1. 我将VS 2010与.NetFramework 3.5一起使用
  2. 我正在使用旧版本的AmazonSDK.dll

解决方案:

在程序中添加网络代理凭据后,它就可以正常工作.

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

注意:

Am使用VS 2010和.NetFramework 3.5 我正在使用旧版本的AmazonSDK.dll

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

这篇关于使用C#从Amazon S3存储桶下载对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆