Search in sources :

Example 6 with AwsServiceException

use of software.amazon.awssdk.awscore.exception.AwsServiceException in project aws-doc-sdk-examples by awsdocs.

the class GetRecommendations method getRecs.

// snippet-start:[personalize.java2.get_recommendations.main]
public static void getRecs(PersonalizeRuntimeClient personalizeRuntimeClient, String campaignArn, String userId) {
    try {
        GetRecommendationsRequest recommendationsRequest = GetRecommendationsRequest.builder().campaignArn(campaignArn).numResults(20).userId(userId).build();
        GetRecommendationsResponse recommendationsResponse = personalizeRuntimeClient.getRecommendations(recommendationsRequest);
        List<PredictedItem> items = recommendationsResponse.itemList();
        for (PredictedItem item : items) {
            System.out.println("Item Id is : " + item.itemId());
            System.out.println("Item score is : " + item.score());
        }
    } catch (AwsServiceException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetRecommendationsRequest(software.amazon.awssdk.services.personalizeruntime.model.GetRecommendationsRequest) PredictedItem(software.amazon.awssdk.services.personalizeruntime.model.PredictedItem) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) GetRecommendationsResponse(software.amazon.awssdk.services.personalizeruntime.model.GetRecommendationsResponse)

Example 7 with AwsServiceException

use of software.amazon.awssdk.awscore.exception.AwsServiceException in project data-transfer-project by google.

the class BackblazeDataTransferClient method uploadFile.

public String uploadFile(String fileKey, File file) throws IOException {
    if (s3Client == null || bucketName == null) {
        throw new IllegalStateException("BackblazeDataTransferClient has not been initialised");
    }
    try {
        long contentLength = file.length();
        monitor.debug(() -> String.format("Uploading '%s' with file size %d bytes", fileKey, contentLength));
        if (contentLength >= sizeThresholdForMultipartUpload) {
            monitor.debug(() -> String.format("File size is larger than %d bytes, so using multipart upload", sizeThresholdForMultipartUpload));
            return uploadFileUsingMultipartUpload(fileKey, file, contentLength);
        }
        PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(bucketName).key(fileKey).build();
        PutObjectResponse putObjectResponse = s3Client.putObject(putObjectRequest, RequestBody.fromFile(file));
        return putObjectResponse.versionId();
    } catch (AwsServiceException | SdkClientException e) {
        throw new IOException(String.format("Error while uploading file, fileKey: %s", fileKey), e);
    }
}
Also used : SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) IOException(java.io.IOException) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest)

Example 8 with AwsServiceException

use of software.amazon.awssdk.awscore.exception.AwsServiceException in project data-transfer-project by google.

the class BackblazeDataTransferClient method getOrCreateBucket.

private String getOrCreateBucket(S3Client s3Client, ListBucketsResponse listBucketsResponse, String region, String exportService) throws IOException {
    String fullPrefix = String.format(DATA_TRANSFER_BUCKET_PREFIX_FORMAT_STRING, exportService.toLowerCase());
    try {
        for (Bucket bucket : listBucketsResponse.buckets()) {
            if (bucket.name().startsWith(fullPrefix)) {
                return bucket.name();
            }
        }
        for (int i = 0; i < MAX_BUCKET_CREATION_ATTEMPTS; i++) {
            String bucketName = String.format("%s-%s", fullPrefix, RandomStringUtils.randomNumeric(8).toLowerCase());
            try {
                CreateBucketConfiguration createBucketConfiguration = CreateBucketConfiguration.builder().locationConstraint(region).build();
                CreateBucketRequest createBucketRequest = CreateBucketRequest.builder().bucket(bucketName).createBucketConfiguration(createBucketConfiguration).build();
                s3Client.createBucket(createBucketRequest);
                return bucketName;
            } catch (BucketAlreadyExistsException | BucketAlreadyOwnedByYouException e) {
                monitor.info(() -> "Bucket name already exists");
            }
        }
        throw new IOException(String.format("Failed to create a uniquely named bucket after %d attempts", MAX_BUCKET_CREATION_ATTEMPTS));
    } catch (AwsServiceException | SdkClientException e) {
        throw new IOException("Error while creating bucket", e);
    }
}
Also used : CreateBucketConfiguration(software.amazon.awssdk.services.s3.model.CreateBucketConfiguration) BucketAlreadyOwnedByYouException(software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException) BucketAlreadyExistsException(software.amazon.awssdk.services.s3.model.BucketAlreadyExistsException) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) Bucket(software.amazon.awssdk.services.s3.model.Bucket) CreateBucketRequest(software.amazon.awssdk.services.s3.model.CreateBucketRequest) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) IOException(java.io.IOException)

Aggregations

AwsServiceException (software.amazon.awssdk.awscore.exception.AwsServiceException)8 GetRecommendationsRequest (software.amazon.awssdk.services.personalizeruntime.model.GetRecommendationsRequest)3 GetRecommendationsResponse (software.amazon.awssdk.services.personalizeruntime.model.GetRecommendationsResponse)3 PredictedItem (software.amazon.awssdk.services.personalizeruntime.model.PredictedItem)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)2 FlywayException (org.flywaydb.core.api.FlywayException)1 ResponseInputStream (software.amazon.awssdk.core.ResponseInputStream)1 DeleteIdentityPoolRequest (software.amazon.awssdk.services.cognitoidentity.model.DeleteIdentityPoolRequest)1 Event (software.amazon.awssdk.services.personalizeevents.model.Event)1 PutEventsRequest (software.amazon.awssdk.services.personalizeevents.model.PutEventsRequest)1 S3Client (software.amazon.awssdk.services.s3.S3Client)1 Bucket (software.amazon.awssdk.services.s3.model.Bucket)1 BucketAlreadyExistsException (software.amazon.awssdk.services.s3.model.BucketAlreadyExistsException)1 BucketAlreadyOwnedByYouException (software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException)1 CreateBucketConfiguration (software.amazon.awssdk.services.s3.model.CreateBucketConfiguration)1 CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)1 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)1 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)1