I don’t think you’ll be able to handle all cases in one go.
If you want to make it generic I would suggest creating a generic interface or abstract class, and then create implementations for each storage that you want to support.
As for the specifics, I’ve forgotten how to do it for S3 but I’m sure you’ll find it in their documentation, for the Mendix storage you can use the following code to get an input stream (not sure why it’s an input stream that you get returned from an output method) which you can then convert to an output:
The full documentation for this is here.
Hope this helps