Search in sources :

Example 1 with SdkException

use of software.amazon.awssdk.core.exception.SdkException in project aws-sdk-java-v2 by aws.

the class RetryOnExceptionsCondition method shouldRetry.

/**
 * @param context Context about the state of the last request and information about the number of requests made.
 * @return True if the exception class matches one of the whitelisted exceptions or if the cause of the exception matches the
 *     whitelisted exception.
 */
@Override
public boolean shouldRetry(RetryPolicyContext context) {
    SdkException exception = context.exception();
    if (exception == null) {
        return false;
    }
    Predicate<Class<? extends Exception>> isRetryableException = ex -> ex.isAssignableFrom(exception.getClass());
    Predicate<Class<? extends Exception>> hasRetrableCause = ex -> exception.getCause() != null && ex.isAssignableFrom(exception.getCause().getClass());
    return exceptionsToRetryOn.stream().anyMatch(isRetryableException.or(hasRetrableCause));
}
Also used : HashSet(java.util.HashSet) Arrays(java.util.Arrays) SdkPublicApi(software.amazon.awssdk.annotations.SdkPublicApi) Predicate(java.util.function.Predicate) RetryPolicyContext(software.amazon.awssdk.core.retry.RetryPolicyContext) Set(java.util.Set) SdkException(software.amazon.awssdk.core.exception.SdkException) ToString(software.amazon.awssdk.utils.ToString) Collectors(java.util.stream.Collectors) SdkException(software.amazon.awssdk.core.exception.SdkException) SdkException(software.amazon.awssdk.core.exception.SdkException)

Example 2 with SdkException

use of software.amazon.awssdk.core.exception.SdkException in project djl by deepjavalibrary.

the class SageMakerTest method testDeployModel.

@Test
public void testDeployModel() throws IOException, ModelException {
    if (!Boolean.getBoolean("nightly") || !hasCredential()) {
        throw new SkipException("The test requires AWS credentials.");
    }
    Criteria<NDList, NDList> criteria = Criteria.builder().setTypes(NDList.class, NDList.class).optModelUrls("https://resources.djl.ai/test-models/mlp.tar.gz").build();
    try (ZooModel<NDList, NDList> model = criteria.loadModel()) {
        SageMaker sageMaker = SageMaker.builder().setModel(model).optBucketName("djl-sm-test").optModelName("resnet").optContainerImage("125045733377.dkr.ecr.us-east-1.amazonaws.com/djl").optExecutionRole("arn:aws:iam::125045733377:role/service-role/DJLSageMaker-ExecutionRole-20210213T1027050").build();
        sageMaker.deploy();
        byte[] image;
        Path imagePath = Paths.get("../../examples/src/test/resources/0.png");
        try (InputStream is = Files.newInputStream(imagePath)) {
            image = Utils.toByteArray(is);
        }
        String ret = new String(sageMaker.invoke(image), StandardCharsets.UTF_8);
        Type type = new TypeToken<List<Classifications.Classification>>() {
        }.getType();
        List<Classifications.Classification> list = JsonUtils.GSON.fromJson(ret, type);
        String className = list.get(0).getClassName();
        Assert.assertEquals(className, "0");
        sageMaker.deleteEndpoint();
        sageMaker.deleteEndpointConfig();
        sageMaker.deleteSageMakerModel();
    } catch (SdkException e) {
        throw new SkipException("Skip tests that requires permission.", e);
    }
}
Also used : Path(java.nio.file.Path) Classifications(ai.djl.modality.Classifications) InputStream(java.io.InputStream) NDList(ai.djl.ndarray.NDList) Type(java.lang.reflect.Type) SdkException(software.amazon.awssdk.core.exception.SdkException) NDList(ai.djl.ndarray.NDList) List(java.util.List) SkipException(org.testng.SkipException) Test(org.testng.annotations.Test)

Example 3 with SdkException

use of software.amazon.awssdk.core.exception.SdkException in project jmix by jmix-framework.

the class AwsFileStorage method removeFile.

@Override
public void removeFile(FileRef reference) {
    try {
        S3Client s3Client = s3ClientReference.get();
        DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucket).key(reference.getPath()).build();
        s3Client.deleteObject(deleteObjectRequest);
    } catch (SdkException e) {
        log.error("Error removing file from S3 storage", e);
        String message = String.format("Could not delete file %s.", reference.getFileName());
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, message);
    }
}
Also used : DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) SdkException(software.amazon.awssdk.core.exception.SdkException) S3Client(software.amazon.awssdk.services.s3.S3Client) FileStorageException(io.jmix.core.FileStorageException)

Example 4 with SdkException

use of software.amazon.awssdk.core.exception.SdkException in project jmix by jmix-framework.

the class AwsFileStorage method saveStream.

@Override
public FileRef saveStream(String fileName, InputStream inputStream) {
    String fileKey = createFileKey(fileName);
    int s3ChunkSizeBytes = this.chunkSize * 1024;
    try (BufferedInputStream bos = new BufferedInputStream(inputStream, s3ChunkSizeBytes)) {
        S3Client s3Client = s3ClientReference.get();
        int totalSizeBytes = bos.available();
        if (totalSizeBytes == 0) {
            s3Client.putObject(PutObjectRequest.builder().bucket(bucket).key(fileKey).build(), RequestBody.empty());
            return new FileRef(getStorageName(), fileKey, fileName);
        }
        CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder().bucket(bucket).key(fileKey).build();
        CreateMultipartUploadResponse response = s3Client.createMultipartUpload(createMultipartUploadRequest);
        List<CompletedPart> completedParts = new ArrayList<>();
        for (int partNumber = 1, readBytes = 0; readBytes != totalSizeBytes; partNumber++) {
            byte[] chunkBytes = new byte[Math.min(totalSizeBytes - readBytes, s3ChunkSizeBytes)];
            readBytes += bos.read(chunkBytes);
            UploadPartRequest uploadPartRequest = UploadPartRequest.builder().bucket(bucket).key(fileKey).uploadId(response.uploadId()).partNumber(partNumber).build();
            String eTag = s3Client.uploadPart(uploadPartRequest, RequestBody.fromBytes(chunkBytes)).eTag();
            CompletedPart part = CompletedPart.builder().partNumber(partNumber).eTag(eTag).build();
            completedParts.add(part);
        }
        CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder().parts(completedParts).build();
        CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder().bucket(bucket).key(fileKey).uploadId(response.uploadId()).multipartUpload(completedMultipartUpload).build();
        s3Client.completeMultipartUpload(completeMultipartUploadRequest);
        return new FileRef(getStorageName(), fileKey, fileName);
    } catch (IOException | SdkException e) {
        log.error("Error saving file to S3 storage", e);
        String message = String.format("Could not save file %s.", fileName);
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, message);
    }
}
Also used : CompletedPart(software.amazon.awssdk.services.s3.model.CompletedPart) ArrayList(java.util.ArrayList) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) IOException(java.io.IOException) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) FileStorageException(io.jmix.core.FileStorageException) BufferedInputStream(java.io.BufferedInputStream) FileRef(io.jmix.core.FileRef) SdkException(software.amazon.awssdk.core.exception.SdkException) S3Client(software.amazon.awssdk.services.s3.S3Client) CompletedMultipartUpload(software.amazon.awssdk.services.s3.model.CompletedMultipartUpload) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)

Example 5 with SdkException

use of software.amazon.awssdk.core.exception.SdkException in project jmix by jmix-framework.

the class AwsFileStorage method openStream.

@Override
public InputStream openStream(FileRef reference) {
    InputStream is;
    try {
        S3Client s3Client = s3ClientReference.get();
        GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucket).key(reference.getPath()).build();
        is = s3Client.getObject(getObjectRequest, ResponseTransformer.toInputStream());
    } catch (SdkException e) {
        log.error("Error loading file from S3 storage", e);
        String message = String.format("Could not load file %s.", reference.getFileName());
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, message);
    }
    return is;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) SdkException(software.amazon.awssdk.core.exception.SdkException) S3Client(software.amazon.awssdk.services.s3.S3Client) FileStorageException(io.jmix.core.FileStorageException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Aggregations

SdkException (software.amazon.awssdk.core.exception.SdkException)10 IOException (java.io.IOException)4 FileStorageException (io.jmix.core.FileStorageException)3 S3Client (software.amazon.awssdk.services.s3.S3Client)3 BufferedInputStream (java.io.BufferedInputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 RetryableException (software.amazon.awssdk.core.exception.RetryableException)2 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)2 Classifications (ai.djl.modality.Classifications)1 NDList (ai.djl.ndarray.NDList)1 Artifact (ai.djl.repository.Artifact)1 MRL (ai.djl.repository.MRL)1 Metadata (ai.djl.repository.Metadata)1 FileRef (io.jmix.core.FileRef)1 Type (java.lang.reflect.Type)1 Path (java.nio.file.Path)1 Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1