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
Common Weakness Enumeration (CWE) external icon

Noncompliant example

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

Compliant example

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