AWS object presence check

High
An object existence check is performed manually. Use built-in operations instead.
Detector ID
java/object-presence@v1.0
Category
Code Quality
Tags
  • aws-java-sdk
  • maintainability
  • efficiency
Common Weakness Enumeration (CWE) 

Noncompliant example

  public boolean checkS3ObjectNoncompliant(AmazonS3 s3Client, String bucketName, String key) {
    // Noncompliant: implements an object existence check from scratch instead of using doesObjectExist.
    try {
        s3Client.getObjectMetadata(bucketName, key);
        return true;
    } catch (Exception e) {
        return false;
    }
}

Compliant example

  public boolean checkS3ObjectCompliant(AmazonS3 s3Client, String bucketName, String key) {
    // Compliant: uses doesObjectExist instead of implementing it from scratch.
    try {
        return s3Client.doesObjectExist(bucketName, key);
    } catch (Exception e) {
        return false;
    }
}