AWS .NET PutObjectRequest使用错误的主机

时间:2016-02-09 20:28:41

标签: c# asp.net amazon-web-services amazon-s3 runtime-error

我正在将Amazon Web Services的.NET库用于将图像上传到Amazon S3存储桶的应用程序。它用于ASP.NET 4.5应用程序的内部服务。 NuGet包名称是AWSSDK,其版本是最新的(截至写作)稳定版:2.3.54.2

当我尝试在PutObjectRequest对象上使用PutObject方法(上传图像blob)时,它会抛出异常并抱怨主机名错误。

var accessKey = Config.GetValue("AWSAccessKey");
var secretKey = Config.GetValue("AWSSecretKey");
using (var client = new AmazonS3Client(accessKey, secretKey, config))
{
            var request = new PutObjectRequest();
            request.BucketName = Config.GetValue("PublicBucket");
            request.Key = newFileName;
            request.InputStream = resizedImage;
            request.AutoCloseStream = false;
            using (var uploadTaskResult = client.PutObject(request))
            {
                using (var uploadStream = uploadTaskResult.ResponseStream)
                {
                    uploadStream.Seek(0, SeekOrigin.Begin);
                    var resultStr = new StreamReader(uploadStream).ReadToEnd();
                }
            }
}

例外情况如下:

Fatal unhandled exception in Web API component: System.Net.WebException: The remote name could not be resolved: 'images.ourcompany.com.http'
   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()
   at Amazon.S3.AmazonS3Client.getRequestStreamCallback[T](IAsyncResult result)
   at Amazon.S3.AmazonS3Client.endOperation[T](IAsyncResult result)
   at Amazon.S3.AmazonS3Client.EndPutObject(IAsyncResult asyncResult)
   at Tracks.Application.Services.Bp.BpTemplateService.UploadImage(Byte[] image, String fileName) in ...

我试图通过逐步调试代码在VS中调试它,但AWSSDK没有附带调试符号。应该注意的是远程主机名(或我认为亚马逊称之为的桶名)是images.ourcompany.com(不是我们真正的公司名称!)。我检查了Config.GetValue("PublicBucket")的值,它确实是images.ourcompany.com。在这个阶段,我已经用尽了对亚马逊S3的有限知识,并且没有关于导致异常被抛出的原因的理论。

1 个答案:

答案 0 :(得分:0)

我认为您必须添加区域端点或/并设置ServiceUrl以建立与AmazonS3的连接,请检查以下类似问题: Coping folder inside AmazonS3 Bucket (c#) Upload images on Amazon S3. source code

 AmazonS3Config cfg = new AmazonS3Config();  
    cfg.RegionEndpoint = Amazon.RegionEndpoint.SAEast1;//your region Endpoint
    string butcketName = "yourBucketName";
    AmazonS3Client s3Client = new AmazonS3Client("your access key",
     "your secret key", cfg);
     PutObjectRequest request = new PutObjectRequest()
                    {
                        BucketName = _bucket,
                        InputStream = stream,
                        Key = fullName
                    };

                    s3Client.PutObject(request);

or

    AmazonS3Config asConfig = new AmazonS3Config()
    {
        ServiceURL = "http://irisdb.s3-ap-southeast2.amazonaws.com/",
        RegionEndpoint = Amazon.RegionEndpoint.APSoutheast2
    };