Search in sources :

Example 91 with AmazonClientException

use of com.amazonaws.AmazonClientException in project ats-framework by Axway.

the class S3Operations method listBucket.

/**
 * @param folderPrefix
 * @param searchString what pattern to be matched. If null it means all, i.e. ".*"
 * @param recursive
 *
 * @return
 * @throws S3OperationException in case of an error from server
 */
private List<S3ObjectInfo> listBucket(String folderPrefix, String searchString, boolean recursive) {
    List<S3ObjectInfo> allListElements = new ArrayList<S3ObjectInfo>();
    // Alternative but not documented in S3 API: getClient().listObjectsV2(bucket, "prefix")
    ListObjectsRequest request = new ListObjectsRequest(bucketName, folderPrefix, null, recursive ? null : "/", null);
    try {
        ObjectListing objectListing = s3Client.listObjects(request);
        int i = 0;
        if (searchString == null) {
            // any string
            searchString = ".*";
        }
        Pattern searchStringPattern = Pattern.compile(searchString);
        while (true) {
            for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator.hasNext(); ) {
                S3ObjectSummary objectSummary = (S3ObjectSummary) iterator.next();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("listObjects(" + (++i) + "): " + objectSummary.toString());
                }
                String[] fileTokens = objectSummary.getKey().split("/");
                String s3Object = fileTokens[fileTokens.length - 1];
                Matcher matcher = searchStringPattern.matcher(s3Object);
                if (matcher.find()) {
                    allListElements.add(new S3ObjectInfo(objectSummary));
                }
            }
            // more objectListing retrieve?
            if (objectListing.isTruncated()) {
                objectListing = s3Client.listNextBatchOfObjects(objectListing);
            } else {
                break;
            }
        }
    } catch (AmazonClientException e) {
        throw new S3OperationException(e);
    }
    return allListElements;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest)

Example 92 with AmazonClientException

use of com.amazonaws.AmazonClientException in project gradle by gradle.

the class S3Client method putMultiPartObject.

private void putMultiPartObject(InputStream inputStream, Long contentLength, URI destination) {
    try {
        S3RegionalResource s3RegionalResource = new S3RegionalResource(destination);
        String bucketName = s3RegionalResource.getBucketName();
        String s3BucketKey = s3RegionalResource.getKey();
        configureClient(s3RegionalResource);
        List<PartETag> partETags = new ArrayList<>();
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, s3BucketKey).withCannedACL(CannedAccessControlList.BucketOwnerFullControl);
        InitiateMultipartUploadResult initResponse = amazonS3Client.initiateMultipartUpload(initRequest);
        try {
            long filePosition = 0;
            long partSize = s3ConnectionProperties.getPartSize();
            LOGGER.debug("Attempting to put resource:[{}] into s3 bucket [{}]", s3BucketKey, bucketName);
            for (int partNumber = 1; filePosition < contentLength; partNumber++) {
                partSize = Math.min(partSize, contentLength - filePosition);
                UploadPartRequest uploadPartRequest = new UploadPartRequest().withBucketName(bucketName).withKey(s3BucketKey).withUploadId(initResponse.getUploadId()).withPartNumber(partNumber).withPartSize(partSize).withInputStream(inputStream);
                partETags.add(amazonS3Client.uploadPart(uploadPartRequest).getPartETag());
                filePosition += partSize;
            }
            CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(bucketName, s3BucketKey, initResponse.getUploadId(), partETags);
            amazonS3Client.completeMultipartUpload(completeRequest);
        } catch (AmazonClientException e) {
            amazonS3Client.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, s3BucketKey, initResponse.getUploadId()));
            throw e;
        }
    } catch (AmazonClientException e) {
        throw ResourceExceptions.putFailed(destination, e);
    }
}
Also used : InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest) AbortMultipartUploadRequest(com.amazonaws.services.s3.model.AbortMultipartUploadRequest) PartETag(com.amazonaws.services.s3.model.PartETag) CompleteMultipartUploadRequest(com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)

Example 93 with AmazonClientException

use of com.amazonaws.AmazonClientException in project apex-malhar by apache.

the class AbstractKinesisOutputOperator method flushRecords.

private void flushRecords() {
    try {
        PutRecordsRequest putRecordsRequest = new PutRecordsRequest();
        putRecordsRequest.setStreamName(streamName);
        putRecordsRequest.setRecords(putRecordsRequestEntryList);
        client.putRecords(putRecordsRequest);
        putRecordsRequestEntryList.clear();
        logger.debug("Records flushed.");
    } catch (AmazonClientException e) {
        logger.warn("PutRecordsRequest exception.", e);
        throw new RuntimeException(e);
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) PutRecordsRequest(com.amazonaws.services.kinesis.model.PutRecordsRequest)

Example 94 with AmazonClientException

use of com.amazonaws.AmazonClientException in project apex-malhar by apache.

the class AbstractKinesisOutputOperator method addRecord.

private void addRecord(T tuple) {
    try {
        Pair<String, V> keyValue = tupleToKeyValue(tuple);
        PutRecordsRequestEntry putRecordsEntry = new PutRecordsRequestEntry();
        putRecordsEntry.setData(ByteBuffer.wrap(getRecord(keyValue.second)));
        putRecordsEntry.setPartitionKey(keyValue.first);
        putRecordsRequestEntryList.add(putRecordsEntry);
    } catch (AmazonClientException e) {
        throw new RuntimeException(e);
    }
}
Also used : PutRecordsRequestEntry(com.amazonaws.services.kinesis.model.PutRecordsRequestEntry) AmazonClientException(com.amazonaws.AmazonClientException)

Example 95 with AmazonClientException

use of com.amazonaws.AmazonClientException in project apex-malhar by apache.

the class KinesisUtil method getRecords.

/**
 * Get the records from the particular shard
 * @param streamName Name of the stream from where the records to be accessed
 * @param recordsLimit Number of records to return from shard
 * @param shId Shard Id of the shard
 * @param iteratorType Shard iterator type
 * @param seqNo Record sequence number
 * @return the list of records from the given shard
 * @throws AmazonClientException
 */
public List<Record> getRecords(String streamName, Integer recordsLimit, String shId, ShardIteratorType iteratorType, String seqNo) throws AmazonClientException {
    assert client != null : "Illegal client";
    try {
        // Create the GetShardIteratorRequest instance and sets streamName, shardId and iteratorType to it
        GetShardIteratorRequest iteratorRequest = new GetShardIteratorRequest();
        iteratorRequest.setStreamName(streamName);
        iteratorRequest.setShardId(shId);
        iteratorRequest.setShardIteratorType(iteratorType);
        // If the iteratorType is AFTER_SEQUENCE_NUMBER, set the sequence No to the iteratorRequest
        if (ShardIteratorType.AFTER_SEQUENCE_NUMBER.equals(iteratorType) || ShardIteratorType.AT_SEQUENCE_NUMBER.equals(iteratorType)) {
            iteratorRequest.setStartingSequenceNumber(seqNo);
        }
        // Get the Response from the getShardIterator service method & get the shardIterator from that response
        GetShardIteratorResult iteratorResponse = client.getShardIterator(iteratorRequest);
        // getShardIterator() specifies the position in the shard
        String iterator = iteratorResponse.getShardIterator();
        // Create the GetRecordsRequest instance and set the recordsLimit and iterator
        GetRecordsRequest getRequest = new GetRecordsRequest();
        getRequest.setLimit(recordsLimit);
        getRequest.setShardIterator(iterator);
        // Get the Response from the getRecords service method and get the data records from that response.
        GetRecordsResult getResponse = client.getRecords(getRequest);
        return getResponse.getRecords();
    } catch (AmazonClientException e) {
        throw new RuntimeException(e);
    }
}
Also used : GetRecordsResult(com.amazonaws.services.kinesis.model.GetRecordsResult) AmazonClientException(com.amazonaws.AmazonClientException) GetShardIteratorRequest(com.amazonaws.services.kinesis.model.GetShardIteratorRequest) GetShardIteratorResult(com.amazonaws.services.kinesis.model.GetShardIteratorResult) GetRecordsRequest(com.amazonaws.services.kinesis.model.GetRecordsRequest)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)202 IOException (java.io.IOException)70 AmazonServiceException (com.amazonaws.AmazonServiceException)32 ArrayList (java.util.ArrayList)32 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)23 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)19 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)17 HashMap (java.util.HashMap)16 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)14 Test (org.junit.Test)14 SienaException (siena.SienaException)12 AWSCredentials (com.amazonaws.auth.AWSCredentials)11 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)11 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)11 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)11 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)10 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)10 InterruptedIOException (java.io.InterruptedIOException)10 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)9 File (java.io.File)9