When you’re using the Amazon S3 client, have you come across the occasional exception that says something like one of these exception messages:
“The request was aborted: The connection was closed unexpectedly”
“Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall”
“Unable to read data from the transport connection: An established connection was aborted by the software in your host machine “
If you do, then you’re probably attempting to return the response stream directly back to the rest of your application with something like this:
However, because the stream is coming from the Amazon S3 service and is fed to your code in chunks, your code needs to ensure that the connection to S3 stays open until all the data has been received. So as mentioned in the S3 documentation (which incidentally, most of us don’t read in great details…) here, you should be wrapping the response you get from the GetObject method in a using clause.
Depends on what it is you want to do with the stream, you might have to handle it differently. For instance, if you just want to read the string content of a text file, you might want to do this:
Alternatively, if you want to return the response stream itself, you’ll need to first load the stream in its entirety and return the loaded stream. Unfortunately, at the time of this writing, the AWSSDK library still hasn’t been migrated to .Net 4 and therefore doesn’t have the uber useful CopyTo method added in .Net 4, so you will most likely have to do the heavy lifting yourself and read the data out manually into a memory stream: