Fix NPE for s3.putObject with files using code from aws-java-sdk.

The code is mostly just lifted from the actual aws-java-sdk:

https://github.com/aws/aws-sdk-java/blob/master/src/main/java/com/amazonaws/services/s3/AmazonS3Client.java#L1263