AWSSDK 2.0.2中的PutObjectRequest异常

时间:2013-11-29 22:42:19

标签: c# asp.net-mvc amazon-web-services

升级到AWSSDK 2.0.2后,此代码块不再将我的流上传到我的s3存储桶。我降级到1.5.39,它运作得很好。谁知道我做错了什么?

using (var s3Client = AWSClientFactory.CreateAmazonS3Client())
        {
            PutObjectRequest putObjectRequest = new PutObjectRequest( );
            putObjectRequest.BucketName = Settings.Default.AWSImageStore;
            putObjectRequest.CannedACL = S3CannedACL.PublicRead;
            putObjectRequest.Key = fileName;
            putObjectRequest.InputStream = imageStream;
            PutObjectResponse response = s3Client.PutObject(putObjectRequest);
            response.Dispose();
        }

我得到一个通用的亚马逊异常,内部异常说“在写入所有字节之前无法关闭流。”

1 个答案:

答案 0 :(得分:3)

当您写入流时,默认情况下流的指针将保留在末尾。在SDK的1.5版本中,SDK将指针移回到您的开头。在2.0中,您需要在调用PutObjectResponse:

之前回头查看
using (var s3Client = AWSClientFactory.CreateAmazonS3Client())
    {
        if(imageStream.CanSeek)
        {
            imageStream.Seek(0,SeekOrigin.Begin);
        }
        PutObjectRequest putObjectRequest = new PutObjectRequest( );
        putObjectRequest.BucketName = Settings.Default.AWSImageStore;
        putObjectRequest.CannedACL = S3CannedACL.PublicRead;
        putObjectRequest.Key = fileName;
        putObjectRequest.InputStream = imageStream;
        PutObjectResponse response = s3Client.PutObject(putObjectRequest);
        response.Dispose();
    }