Search in sources :

Example 26 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest in project zeppelin by apache.

the class S3NotebookRepo method list.

@Override
public Map<String, NoteInfo> list(AuthenticationInfo subject) throws IOException {
    Map<String, NoteInfo> notesInfo = new HashMap<>();
    try {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(user + "/" + "notebook");
        ObjectListing objectListing;
        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                if (objectSummary.getKey().endsWith(".zpln")) {
                    try {
                        NoteInfo info = getNoteInfo(objectSummary.getKey());
                        notesInfo.put(info.getId(), info);
                    } catch (IOException e) {
                        LOGGER.warn(e.getMessage());
                    }
                }
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (AmazonClientException ace) {
        throw new IOException("Fail to list objects in S3", ace);
    }
    return notesInfo;
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) NoteInfo(org.apache.zeppelin.notebook.NoteInfo) HashMap(java.util.HashMap) AmazonClientException(com.amazonaws.AmazonClientException) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) IOException(java.io.IOException)

Example 27 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest in project zeppelin by apache.

the class S3NotebookRepo method move.

@Override
public void move(String folderPath, String newFolderPath, AuthenticationInfo subject) throws IOException {
    try {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(rootFolder + folderPath + "/");
        ObjectListing objectListing;
        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                if (objectSummary.getKey().endsWith(".zpln")) {
                    String noteId = getNoteId(objectSummary.getKey());
                    String notePath = getNotePath(rootFolder, objectSummary.getKey());
                    String newNotePath = newFolderPath + notePath.substring(folderPath.length());
                    move(noteId, notePath, newNotePath, subject);
                }
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (AmazonClientException ace) {
        throw new IOException("Fail to move folder: " + folderPath + " to " + newFolderPath + " in S3", ace);
    }
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) AmazonClientException(com.amazonaws.AmazonClientException) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) IOException(java.io.IOException)

Example 28 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest in project cloudstack by apache.

the class S3Utils method listDirectory.

public static List<S3ObjectSummary> listDirectory(final ClientOptions clientOptions, final String bucketName, final String directory) {
    LOGGER.debug(format("Listing S3 directory %1$s in bucket %2$s", directory, bucketName));
    List<S3ObjectSummary> objects = new ArrayList<>();
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
    listObjectsRequest.withBucketName(bucketName);
    listObjectsRequest.withPrefix(directory);
    ObjectListing ol = getAmazonS3Client(clientOptions).listObjects(listObjectsRequest);
    if (ol.isTruncated()) {
        do {
            objects.addAll(ol.getObjectSummaries());
            listObjectsRequest.setMarker(ol.getNextMarker());
            ol = getAmazonS3Client(clientOptions).listObjects(listObjectsRequest);
        } while (ol.isTruncated());
    } else {
        objects.addAll(ol.getObjectSummaries());
    }
    if (objects.isEmpty()) {
        return emptyList();
    }
    return unmodifiableList(objects);
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ArrayList(java.util.ArrayList) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary)

Example 29 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest 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. &quot;.*&quot;
 * @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 30 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest in project gradle by gradle.

the class S3Client method listDirectChildren.

public List<String> listDirectChildren(URI parent) {
    S3RegionalResource s3RegionalResource = new S3RegionalResource(parent);
    String bucketName = s3RegionalResource.getBucketName();
    String s3BucketKey = s3RegionalResource.getKey();
    configureClient(s3RegionalResource);
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(s3BucketKey).withMaxKeys(1000).withDelimiter("/");
    ObjectListing objectListing = amazonS3Client.listObjects(listObjectsRequest);
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    builder.addAll(resourceResolver.resolveResourceNames(objectListing));
    while (objectListing.isTruncated()) {
        objectListing = amazonS3Client.listNextBatchOfObjects(objectListing);
        builder.addAll(resourceResolver.resolveResourceNames(objectListing));
    }
    return builder.build();
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ImmutableList(com.google.common.collect.ImmutableList) ObjectListing(com.amazonaws.services.s3.model.ObjectListing)

Aggregations

ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)48 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)46 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)32 ArrayList (java.util.ArrayList)23 AmazonClientException (com.amazonaws.AmazonClientException)11 IOException (java.io.IOException)9 Path (org.apache.hadoop.fs.Path)9 HashMap (java.util.HashMap)8 LocatedFileStatus (org.apache.hadoop.fs.LocatedFileStatus)8 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)6 FileStatus (org.apache.hadoop.fs.FileStatus)6 ListObjectsRequest (software.amazon.awssdk.services.s3.model.ListObjectsRequest)6 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)6 Date (java.util.Date)5 Test (org.junit.Test)5 S3Object (software.amazon.awssdk.services.s3.model.S3Object)5 StocatorPath (com.ibm.stocator.fs.common.StocatorPath)4 FileNotFoundException (java.io.FileNotFoundException)4 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3