Akon
Akon

Reputation: 272

Image not able to uploaded to Amazon S3 server

I am using the below code to upload image file Amazon AWS S3 server..

using (var msImage = new MemoryStream(arrayImage))
using (var msImageL1 = new MemoryStream())
using (var bmImage = (Bitmap)Image.FromStream(msImage))
using (var bmPicture01 = new Bitmap(924, 693))
using (Graphics gPicture01 = Graphics.FromImage(bmPicture01))
{
    using (IAmazonS3 s3client = new AmazonS3Client(accessKey, secretKey, RegionEndpoint.APSoutheast1))
    {
        PutObjectRequest putObjectRequest = new PutObjectRequest
        {
            BucketName = bucketName,
            Key = "sample/5.jpg",
            InputStream = msImage
        };

        s3client.PutObject(putObjectRequest);

    }

    gPicture01.InterpolationMode = InterpolationMode.HighQualityBicubic;
    gPicture01.DrawImage(bmImage, 0, 0, 924, 693);
    bmPicture01.Save(msImageL1, ImageFormat.Jpeg);

    using (IAmazonS3 s3client = new AmazonS3Client(accessKey, secretKey, RegionEndpoint.APSoutheast1))
    {
        PutObjectRequest putObjectRequest = new PutObjectRequest
        {
            BucketName = bucketName,
            Key = "sample/6.jpg",
            InputStream = msImageL1
        };

        s3client.PutObject(putObjectRequest);

    }
}

The first code, which is uploading "5.jpg" is working fine, and uploading successfully. But the other code is now working and giving exception that,

Message=The request was aborted: The request was canceled.
Message=Cannot close stream until all bytes are written.

I am using Amazon Web Services SDK for .NET version 2.0.2.3

Please suggest what might be going wrong here..

Upvotes: 2

Views: 815

Answers (2)

Pavel Safronov
Pavel Safronov

Reputation: 1286

The issue here is that bmPicture01.Save(msImageL1, ImageFormat.Jpeg); saves the bitmap to a string but doesn't reset the position of the stream to 0. You will need to do that before passing the stream to PutObject.

Upvotes: 1

Karn Ratana
Karn Ratana

Reputation: 166

Since there hasn't been any other answer and my first response got deleted because it "does not really answer the question", I'm going to try and rephrase it.

You said:

Please suggest what might be going wrong here..

My suggestion is that there is nothing wrong with you code. Instead it's a possible bug with version 2.0.1 and later. See https://forums.aws.amazon.com/thread.jspa?threadID=139612&tstart=0 (requires log in). I've tried with version 2.0.2.3 and the bug is still there.

You can test this by using a previous version.

Upvotes: 1

Related Questions