I am trying to create a new custom stream wrapper. How can I do that?
This is something I have worked on more than a year ago https://gist.github.com/ivanjaros/fcf7ed50fbb98024464a29cfdcb01167 I have written Google Cloud Storage stream wrapper last week which is much better than this but it is a bit different use case(I think that gist is 95% accurate to what I have now). Anyway, you'll need to provide the S3 service to it on your own. I'd suggest to go over the code and maybe get inspired but I wouldn't use it as is.
Also see this issue: https://www.drupal.org/node/2830828