chemitaxis
chemitaxis

Reputation: 14889

Upload image to S3 in C# ASP.NET MVC 5

I'm trying to upload a image to my bucket, but I can't because I have this error:

An exception of type 'Amazon.Runtime.AmazonServiceException' occurred in mscorlib.dll but was not handled in user code

Detail

{"Encountered a non retryable WebException : RequestCanceled"}

More Detail

{"The request was aborted: The request was canceled."}

Inner Exception

The stream was already consumed. It cannot be read again.

I'm using the AWS SDK for VS2013.

Code

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

Upvotes: 2

Views: 1615

Answers (3)

Kumar Bhimsen
Kumar Bhimsen

Reputation: 1

User as follows :

TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client("Access Key Id", "Access Segret key",Amazon.RegionEndpoint.USEast1));

Upvotes: 0

chemitaxis
chemitaxis

Reputation: 14889

My error was in:

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

I was using an incorrect region... I changed by Europe and it works.

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

Upvotes: 2

nsgocev
nsgocev

Reputation: 4470

By doing this:

using (var fileToUpload =
                new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                fileTransferUtility.Upload(fileToUpload,
                                           ExistingBucketName, KeyName);
            }

You are disposing your filestream before it gets uploaded. Try to remove the using clause or wrap the entire call in it.

Upvotes: 0

Related Questions