Avoid reset exception in Amazon S3 Medium

Uploading objects to Amazon S3 by using streams (either through an AmazonS3 client or TransferManager) might encounter network connectivity or timeout issues. The most reliable way to avoid a ResetException is to provide data by using a File or FileInputStream, which the AWS SDK for Java can handle without being constrained by mark and reset limits.

Detector ID
java/avoid-reset-exception-rule@v1.0
Category

Noncompliant example

1public void s3PutObjectNoncompliant(String bucket, String key, InputStream content,
2                                    ObjectMetadata metadata, AmazonS3 s3Client, String owner) {
3    log.info("Putting content into bucket {} and key {}", bucket, key);
4    // Noncompliant: readLimit not set.
5    PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, content, metadata);
6    putObjectRequest.setExpectedBucketOwner(owner);
7    s3Client.putObject(putObjectRequest);
8}

Compliant example

1public void s3PutObjectCompliant(String bucket, String key, InputStream content,
2                                 ObjectMetadata metadata, AmazonS3 s3Client, String owner) {
3    log.info("Putting content into bucket {} and key {}", bucket, key);
4    PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, content, metadata);
5    putObjectRequest.setExpectedBucketOwner(owner);
6    // Compliant: readLimit is set.
7    putObjectRequest.getRequestClientOptions().setReadLimit(100);
8    s3Client.putObject(putObjectRequest);
9}