use of com.amazonaws.services.s3.model.S3ObjectSummary 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.services.s3.model.S3ObjectSummary in project athenz by yahoo.
the class S3ChangeLogStoreTest method testGetUpdatedSignedDomainsNoChanges.
@Test
public void testGetUpdatedSignedDomainsNoChanges() {
MockS3ChangeLogStore store = new MockS3ChangeLogStore();
ArrayList<S3ObjectSummary> objectList = new ArrayList<>();
S3ObjectSummary objectSummary = new S3ObjectSummary();
objectSummary.setKey("iaas");
objectSummary.setLastModified(new Date(100));
objectList.add(objectSummary);
objectSummary = new S3ObjectSummary();
objectSummary.setKey("iaas.athenz");
objectSummary.setLastModified(new Date(200));
objectList.add(objectSummary);
ObjectListing objectListing = mock(ObjectListing.class);
when(objectListing.getObjectSummaries()).thenReturn(objectList);
when(objectListing.isTruncated()).thenReturn(false);
when(store.awsS3Client.listObjects(any(ListObjectsRequest.class))).thenReturn(objectListing);
// set the last modification time to not return any of the domains
store.lastModTime = (new Date(250)).getTime();
StringBuilder lastModTimeBuffer = new StringBuilder(512);
SignedDomains signedDomains = store.getUpdatedSignedDomains(lastModTimeBuffer);
assertTrue(lastModTimeBuffer.length() > 0);
assertEquals(signedDomains.getDomains().size(), 0);
}
use of com.amazonaws.services.s3.model.S3ObjectSummary in project athenz by yahoo.
the class S3ChangeLogStoreTest method testGetLocalSignedDomains.
@Test
public void testGetLocalSignedDomains() {
MockS3ChangeLogStore store = new MockS3ChangeLogStore();
ArrayList<S3ObjectSummary> objectList = new ArrayList<>();
S3ObjectSummary objectSummary = new S3ObjectSummary();
objectSummary.setKey("iaas");
objectList.add(objectSummary);
objectSummary = new S3ObjectSummary();
objectSummary.setKey("iaas.athenz");
objectList.add(objectSummary);
ObjectListing objectListing = mock(ObjectListing.class);
when(objectListing.getObjectSummaries()).thenReturn(objectList);
when(objectListing.isTruncated()).thenReturn(false);
when(store.awsS3Client.listObjects(any(ListObjectsRequest.class))).thenReturn(objectListing);
// verify that our last mod time is 0 before the call
assertEquals(store.lastModTime, 0);
// retrieve the list of domains
List<String> domains = store.getLocalDomainList();
assertEquals(domains.size(), 2);
assertTrue(domains.contains("iaas"));
assertTrue(domains.contains("iaas.athenz"));
// also verify that last mod time is updated
assertTrue(store.lastModTime > 0);
// get the list again
domains = store.getLocalDomainList();
assertEquals(domains.size(), 2);
assertTrue(domains.contains("iaas"));
assertTrue(domains.contains("iaas.athenz"));
}
use of com.amazonaws.services.s3.model.S3ObjectSummary in project athenz by yahoo.
the class S3ChangeLogStoreTest method testListObjectsAllObjectsErrorCondition.
@Test
public void testListObjectsAllObjectsErrorCondition() {
MockS3ChangeLogStore store = new MockS3ChangeLogStore();
ArrayList<S3ObjectSummary> objectList1 = new ArrayList<>();
S3ObjectSummary objectSummary = new S3ObjectSummary();
objectSummary.setKey("iaas");
objectList1.add(objectSummary);
objectSummary = new S3ObjectSummary();
objectSummary.setKey("iaas.athenz");
objectList1.add(objectSummary);
ArrayList<S3ObjectSummary> objectList2 = new ArrayList<>();
objectSummary = new S3ObjectSummary();
objectSummary.setKey("cd");
objectList2.add(objectSummary);
objectSummary = new S3ObjectSummary();
objectSummary.setKey("cd.docker");
objectList2.add(objectSummary);
ObjectListing objectListing = mock(ObjectListing.class);
when(objectListing.getObjectSummaries()).thenReturn(objectList1).thenReturn(objectList2);
when(objectListing.isTruncated()).thenReturn(true);
when(store.awsS3Client.listObjects(any(ListObjectsRequest.class))).thenReturn(objectListing);
when(store.awsS3Client.listNextBatchOfObjects(any(ObjectListing.class))).thenReturn(objectListing).thenReturn(null);
ArrayList<String> domains = new ArrayList<>();
store.listObjects(store.awsS3Client, domains, 0);
assertEquals(domains.size(), 4);
assertTrue(domains.contains("iaas"));
assertTrue(domains.contains("iaas.athenz"));
assertTrue(domains.contains("cd"));
assertTrue(domains.contains("cd.docker"));
}
use of com.amazonaws.services.s3.model.S3ObjectSummary in project athenz by yahoo.
the class S3ChangeLogStoreTest method testGetServerDomains.
@Test
public void testGetServerDomains() {
MockS3ChangeLogStore store = new MockS3ChangeLogStore();
ArrayList<S3ObjectSummary> objectList = new ArrayList<>();
S3ObjectSummary objectSummary = new S3ObjectSummary();
objectSummary.setKey("iaas");
objectList.add(objectSummary);
objectSummary = new S3ObjectSummary();
objectSummary.setKey("iaas.athenz");
objectList.add(objectSummary);
ObjectListing objectListing = mock(ObjectListing.class);
when(objectListing.getObjectSummaries()).thenReturn(objectList);
when(objectListing.isTruncated()).thenReturn(false);
when(store.awsS3Client.listObjects(any(ListObjectsRequest.class))).thenReturn(objectListing);
// verify that our last mod time is 0 before the call
assertEquals(store.lastModTime, 0);
// retrieve the list of domains
Set<String> domains = store.getServerDomainList();
assertEquals(domains.size(), 2);
assertTrue(domains.contains("iaas"));
assertTrue(domains.contains("iaas.athenz"));
// also verify that last mod time is not updated
assertEquals(store.lastModTime, 0);
}
Aggregations