Inefficient chain of AWS API calls Medium

The code uses a chain of API calls to perform an operation where a single API call is available. Use a single API call instead for efficiency.

Detector ID
java/aws-inefficient-chain@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1private String inefficientApiCallsNoncompliant(final String bucketName, final String key) throws IOException {
2    AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
3    // Noncompliant: uses inefficient chain of API calls over an efficient single API call.
4    S3Object s3object = s3Client.getObject(bucketName, key);
5    try {
6        return s3object.getObjectMetadata().getVersionId();
7    } finally {
8        s3object.close();
9    }
10}

Compliant example

1private String efficientApiCallsCompliant(final String bucketName, final String key) {
2    AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
3    // Compliant: uses an efficient single API call over inefficient chain of API calls.
4    return s3Client.getObjectMetadata(bucketName, key).getVersionId();
5}