Search in sources :

Example 36 with S3Exception

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

the class S3Service method getObjectBytes.

public byte[] getObjectBytes(String bucketName, String keyName) {
    try {
        S3Client s3 = getClient();
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        return data;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) S3Client(software.amazon.awssdk.services.s3.S3Client) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 37 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project pravega by pravega.

the class S3ChunkStorage method checkExists.

@Override
protected boolean checkExists(String chunkName) throws ChunkStorageException {
    try {
        val objectPath = getObjectPath(chunkName);
        val response = client.headObject(HeadObjectRequest.builder().bucket(this.config.getBucket()).key(objectPath).build());
        return true;
    } catch (S3Exception e) {
        if (e.awsErrorDetails().errorCode().equals(NO_SUCH_KEY)) {
            return false;
        } else {
            throw convertException(chunkName, "checkExists", e);
        }
    }
}
Also used : lombok.val(lombok.val) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception)

Example 38 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project pravega by pravega.

the class S3ChunkStorage method convertException.

private ChunkStorageException convertException(String chunkName, String message, Exception e) {
    ChunkStorageException retValue = null;
    if (e instanceof ChunkStorageException) {
        return (ChunkStorageException) e;
    }
    if (e instanceof S3Exception) {
        S3Exception s3Exception = (S3Exception) e;
        String errorCode = Strings.nullToEmpty(s3Exception.awsErrorDetails().errorCode());
        if (errorCode.equals(NO_SUCH_KEY)) {
            retValue = new ChunkNotFoundException(chunkName, message, e);
        }
        if (errorCode.equals(PRECONDITION_FAILED)) {
            retValue = new ChunkAlreadyExistsException(chunkName, message, e);
        }
        if (errorCode.equals(INVALID_RANGE) || errorCode.equals(INVALID_ARGUMENT) || errorCode.equals(METHOD_NOT_ALLOWED) || s3Exception.awsErrorDetails().sdkHttpResponse().statusCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
            throw new IllegalArgumentException(chunkName, e);
        }
        if (errorCode.equals(ACCESS_DENIED)) {
            retValue = new ChunkStorageException(chunkName, String.format("Access denied for chunk %s - %s.", chunkName, message), e);
        }
    }
    if (retValue == null) {
        retValue = new ChunkStorageException(chunkName, message, e);
    }
    return retValue;
}
Also used : ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) ChunkAlreadyExistsException(io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)

Example 39 with S3Exception

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

the class DetectPPE method getObjectBytes.

public static byte[] getObjectBytes(S3Client s3, String bucketName, String keyName) {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        return data;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 40 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception 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

S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)56 ArrayList (java.util.ArrayList)13 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)12 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)11 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)9 IOException (java.io.IOException)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 S3Object (software.amazon.awssdk.services.s3.model.S3Object)6 S3Waiter (software.amazon.awssdk.services.s3.waiters.S3Waiter)5 OutputStream (java.io.OutputStream)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 ListObjectsRequest (software.amazon.awssdk.services.s3.model.ListObjectsRequest)4 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)4 Tag (software.amazon.awssdk.services.s3.model.Tag)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 HashMap (java.util.HashMap)3 CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)3 HeadBucketRequest (software.amazon.awssdk.services.s3.model.HeadBucketRequest)3