0

I have an ASP.NET web application. My goal is to upload files to the S3 storage asynchronously. My plan is to use TransferUtility.UploadAsync or AmazonS3Client.PutObjectAsync. While the documentation doesn't specifically indicate the async wrappers, but they exist (here and here). Each of these async methods returns an awaitable Task. My problem is that neither methods worked for me (the file simply doesn't show in the bucket). My code calls the async method and doesn't wait for the task to be completed. While I'm aware of the ramifications of this design (like the fact that IIS might shut down my task since no request is attached to it), but I'm OK with that. Here is how the code looks like:

public static void AddFileAsync(Stream stream, string key)
{
    using (IAmazonS3 s3Client = AmazonS3ConfigManager.GetAmazonS3Client()) // here I provide the private and public keys and the region endpoint
    using (TransferUtility transferUtility = new TransferUtility(s3Client))
    {
        TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest
        {
            BucketName = "MyBucketName",
            InputStream = stream,
            Key = key,
            AutoCloseStream = false // because I need the stream for later
        };
        transferUtility.UploadAsync(fileTransferUtilityRequest);
    }
}

On the other hand, if I wait for the returned task to be completed, the file does upload successfully. In code, if I do the following, it works:

var task = transferUtility.UploadAsync(fileTransferUtilityRequest);
task.Wait();

Is there a way to perform an asynchronous upload to Amazon S3 within an ASP.NET Web Application (I'm using Web Forms in case that matters)?

2
  • The stream you're passing, is it taken from Request.GetRequestStream() by any chance? If yes, I think it gets destructed when you start a new thread by calling the async method. Commented Feb 14, 2016 at 21:04
  • Good point. No, the stream is not coming from a WebRequest.GetRequestStream(). Also, I made sure that the stream does not get disposed during the async upload. Commented Feb 15, 2016 at 0:37

1 Answer 1

1

I suspect what is happening you call the async upload operation and then the original thread exits out of the using block disposing the s3Client and transferUtility objects that the upload thread is attempting to use to upload the object. You'll need to keep those objects around for the length of the upload thread.

Sign up to request clarification or add additional context in comments.

1 Comment

I put a breakpoint at the UploadAsync method, hit F10, and waited. Nothing showed up in the bucket even though I'm still in the inner using statement (nothing got disposed yet)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.