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;
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations