Search in sources :

Example 1 with NoSuchKeyException

use of software.amazon.awssdk.services.s3.model.NoSuchKeyException in project aws-doc-sdk-examples by awsdocs.

the class MovieLensDatasetProvider method getObjectBytes.

// Checks to see if the dataset is already uploaded to s3.
public static boolean getObjectBytes(S3Client s3Client, String bucketName, String keyName) {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        return data.length > 0;
    } catch (NoSuchKeyException | NoSuchBucketException ex) {
        return false;
    } catch (S3Exception s3Exception) {
        System.err.println(s3Exception.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return false;
}
Also used : NoSuchKeyException(software.amazon.awssdk.services.s3.model.NoSuchKeyException) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) NoSuchBucketException(software.amazon.awssdk.services.s3.model.NoSuchBucketException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Aggregations

GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)1 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)1 NoSuchBucketException (software.amazon.awssdk.services.s3.model.NoSuchBucketException)1 NoSuchKeyException (software.amazon.awssdk.services.s3.model.NoSuchKeyException)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1