Search in sources :

Example 1 with SdkClientException

use of software.amazon.awssdk.core.exception.SdkClientException in project beam by apache.

the class S3ReadableSeekableByteChannel method read.

@Override
public int read(ByteBuffer destinationBuffer) throws IOException {
    if (!isOpen()) {
        throw new ClosedChannelException();
    }
    if (!destinationBuffer.hasRemaining()) {
        return 0;
    }
    if (position == contentLength) {
        return -1;
    }
    if (s3ResponseInputStream == null) {
        GetObjectRequest.Builder builder = GetObjectRequest.builder().bucket(path.getBucket()).key(path.getKey()).sseCustomerKey(config.getSSECustomerKey().getKey()).sseCustomerAlgorithm(config.getSSECustomerKey().getAlgorithm());
        if (position > 0) {
            builder.range(String.format("bytes=%s-%s", position, contentLength));
        }
        GetObjectRequest request = builder.build();
        try {
            s3ResponseInputStream = s3Client.getObject(request);
        } catch (SdkClientException e) {
            throw new IOException(e);
        }
        s3ObjectContentChannel = Channels.newChannel(new BufferedInputStream(s3ResponseInputStream, 1024 * 1024));
    }
    int totalBytesRead = 0;
    int bytesRead = 0;
    do {
        totalBytesRead += bytesRead;
        try {
            bytesRead = s3ObjectContentChannel.read(destinationBuffer);
        } catch (SdkServiceException e) {
            throw new IOException(e);
        }
    } while (bytesRead > 0);
    position += totalBytesRead;
    return totalBytesRead;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) SdkServiceException(software.amazon.awssdk.core.exception.SdkServiceException) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 2 with SdkClientException

use of software.amazon.awssdk.core.exception.SdkClientException in project beam by apache.

the class S3WritableByteChannel method flush.

private void flush() throws IOException {
    uploadBuffer.flip();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(uploadBuffer.array(), 0, uploadBuffer.limit());
    UploadPartRequest request = UploadPartRequest.builder().bucket(path.getBucket()).key(path.getKey()).uploadId(uploadId).partNumber(partNumber++).contentLength((long) uploadBuffer.limit()).sseCustomerKey(config.getSSECustomerKey().getKey()).sseCustomerAlgorithm(config.getSSECustomerKey().getAlgorithm()).sseCustomerKeyMD5(config.getSSECustomerKey().getMD5()).contentMD5(Base64.getEncoder().encodeToString(md5.digest())).build();
    UploadPartResponse response;
    try {
        response = s3Client.uploadPart(request, RequestBody.fromInputStream(inputStream, request.contentLength()));
    } catch (SdkClientException e) {
        throw new IOException(e);
    }
    CompletedPart part = CompletedPart.builder().partNumber(request.partNumber()).eTag(response.eTag()).build();
    uploadBuffer.clear();
    md5.reset();
    completedParts.add(part);
}
Also used : SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) ByteArrayInputStream(java.io.ByteArrayInputStream) CompletedPart(software.amazon.awssdk.services.s3.model.CompletedPart) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) IOException(java.io.IOException) UploadPartResponse(software.amazon.awssdk.services.s3.model.UploadPartResponse)

Example 3 with SdkClientException

use of software.amazon.awssdk.core.exception.SdkClientException in project beam by apache.

the class S3WritableByteChannel method close.

@Override
public void close() throws IOException {
    open = false;
    if (uploadBuffer.remaining() > 0) {
        flush();
    }
    CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder().parts(completedParts).build();
    CompleteMultipartUploadRequest request = CompleteMultipartUploadRequest.builder().bucket(path.getBucket()).key(path.getKey()).uploadId(uploadId).multipartUpload(completedMultipartUpload).build();
    try {
        s3Client.completeMultipartUpload(request);
    } catch (SdkClientException e) {
        throw new IOException(e);
    }
}
Also used : SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) IOException(java.io.IOException) CompletedMultipartUpload(software.amazon.awssdk.services.s3.model.CompletedMultipartUpload) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)

Example 4 with SdkClientException

use of software.amazon.awssdk.core.exception.SdkClientException in project flyway by flyway.

the class AwsS3Scanner method scanForResources.

/**
 * Scans S3 for the resources. In AWS SDK v2, only the region that the client is configured with can be used.
 * The format of the path is expected to be {@code s3:{bucketName}/{optional prefix}}.
 *
 * @param location The location in S3 to start searching. Subdirectories are also searched.
 * @return The resources that were found.
 */
@Override
public Collection<LoadableResource> scanForResources(final Location location) {
    String bucketName = getBucketName(location);
    String prefix = getPrefix(bucketName, location.getPath());
    S3Client s3Client = S3ClientFactory.getClient();
    try {
        ListObjectsV2Request.Builder builder = ListObjectsV2Request.builder().bucket(bucketName).prefix(prefix);
        ListObjectsV2Request request = builder.build();
        ListObjectsV2Response listObjectResult = s3Client.listObjectsV2(request);
        return getLoadableResources(bucketName, listObjectResult);
    } catch (SdkClientException e) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Could not access s3 location:" + bucketName + prefix + " due to error: " + e.getMessage());
        }
        LOG.error("Skipping s3 location:" + bucketName + prefix + " due to error: " + e.getMessage());
        return Collections.emptyList();
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) S3Client(software.amazon.awssdk.services.s3.S3Client) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response)

Example 5 with SdkClientException

use of software.amazon.awssdk.core.exception.SdkClientException 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)

Aggregations

SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)6 IOException (java.io.IOException)5 AwsServiceException (software.amazon.awssdk.awscore.exception.AwsServiceException)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 FlywayException (org.flywaydb.core.api.FlywayException)1 SdkServiceException (software.amazon.awssdk.core.exception.SdkServiceException)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 CompleteMultipartUploadRequest (software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)1 CompletedMultipartUpload (software.amazon.awssdk.services.s3.model.CompletedMultipartUpload)1 CompletedPart (software.amazon.awssdk.services.s3.model.CompletedPart)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 ListObjectsV2Request (software.amazon.awssdk.services.s3.model.ListObjectsV2Request)1 ListObjectsV2Response (software.amazon.awssdk.services.s3.model.ListObjectsV2Response)1