我正在尝试将图像上传到存储桶,但是无法执行,因为我遇到了以下错误:


  mscorlib.dll中发生类型为'Amazon.Runtime.AmazonServiceException'的异常,但未在用户代码中处理


详情


  {“遇到不可重试的WebException:RequestCanceled”}


更多详情


  {“请求被终止:请求被取消。”}


内部异常


  流已被消耗。无法再次读取。


我正在使用VS2013的AWS开发工具包。



private const string ExistingBucketName = "******development"; //Name of the bucket
private const string KeyName = "Images";

public static void UploadToS3(string filePath)
    {

        //filePath -> C:\example.jpg

        try
        {
            var fileTransferUtility = new
                TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));

            // 1. Upload a file, file name is used as the object key name.
            fileTransferUtility.Upload(filePath, ExistingBucketName);
            Trace.WriteLine("Upload 1 completed");

            // 2. Specify object key name explicitly.
            fileTransferUtility.Upload(filePath,
                                      ExistingBucketName, KeyName);
            Trace.WriteLine("Upload 2 completed");

            // 3. Upload data from a type of System.IO.Stream.
            using (var fileToUpload =
                new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                fileTransferUtility.Upload(fileToUpload,
                                           ExistingBucketName, KeyName);
            }
            Trace.WriteLine("Upload 3 completed");

            // 4.Specify advanced settings/options.
            var fileTransferUtilityRequest = new TransferUtilityUploadRequest
            {
                BucketName = ExistingBucketName,
                FilePath = filePath,
                StorageClass = S3StorageClass.ReducedRedundancy,
                PartSize = 6291456, // 6 MB.
                Key = KeyName,
                CannedACL = S3CannedACL.PublicRead
            };
            fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
            fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
            fileTransferUtility.Upload(fileTransferUtilityRequest);
            Trace.WriteLine("Upload 4 completed");
        }
        catch (AmazonS3Exception s3Exception)
        {
            Trace.WriteLine(s3Exception.Message);
            Trace.WriteLine(s3Exception.InnerException);
        }
    }

最佳答案

我的错误是在:

var fileTransferUtility = new
                TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));


我使用的地区不正确...我被欧洲更改了,并且可以正常工作。

 var fileTransferUtility = new
                    TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.EUWest1));

10-08 10:57