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)?
WebRequest.GetRequestStream(). Also, I made sure that the stream does not get disposed during the async upload.