Search in sources :

Example 96 with S3ObjectSummary

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;
}
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 97 with S3ObjectSummary

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);
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) SignedDomains(com.yahoo.athenz.zms.SignedDomains) Test(org.testng.annotations.Test)

Example 98 with S3ObjectSummary

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"));
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) Test(org.testng.annotations.Test)

Example 99 with S3ObjectSummary

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"));
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) Test(org.testng.annotations.Test)

Example 100 with S3ObjectSummary

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);
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) Test(org.testng.annotations.Test)

Aggregations

S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)196 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)106 ArrayList (java.util.ArrayList)64 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)61 Test (org.junit.Test)50 Date (java.util.Date)29 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)27 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)25 Test (org.testng.annotations.Test)25 AmazonS3 (com.amazonaws.services.s3.AmazonS3)23 S3Object (com.amazonaws.services.s3.model.S3Object)19 AmazonClientException (com.amazonaws.AmazonClientException)18 IOException (java.io.IOException)17 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)16 AmazonServiceException (com.amazonaws.AmazonServiceException)14 ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)14 File (java.io.File)13 HashMap (java.util.HashMap)13 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)13 StorageFile (org.finra.herd.model.api.xml.StorageFile)13