I have an ASP.Net Web API application that allows clients (html pages and iPhone apps) to upload images to. I am using an async upload task as described in this article.
Everything works great when I want to save to the file system because that's what this code does automatically, behind the scenes it seems. But, I don't want to save the uploaded files to the file system. Instead, I want to take the uploaded stream and pass it through to an Amazon S3 bucket using the AWS SDK for .Net.
I have the code set up to send the stream up to AWS. The problem I can't figure out is how to get the uploaded content stream from the Web API method instead of having it automatically save to disk.
I was hoping there would be a virtual method I could override in MultipartFormDataStreamProvider which would allow me to do something else with the uploaded content other than save to disk, but there doesn't seem to be.
Any suggestions?
You could override MultipartFormDataStreamProvider's GetStream method to return a stream which is not a file stream but your AWS stream, but there are some issues doing so(which I will not elaborate here). Instead you could create a provider deriving from the abstract base class MultipartStreamProvider. Following sample is heavily based on the actual source code of MultipartFormDataStreamProvider and MultipartFileStreamProvider. You can check here and here for more details. Sample below:
Since @KiranChalla posted their answer, a new abstract class
MultipartFormDataRemoteStreamProvider
was introduced in Fix 1760: Make MultipartFormDataStreamProvider easier to work with non-FileStreams. to make this easier.The summary of the class does a good job at explaining how to use it: