Search in sources :

Example 1 with ListObjectsRequest

use of com.amazonaws.services.s3.model.ListObjectsRequest in project hadoop by apache.

the class S3AFileSystem method innerListStatus.

/**
   * List the statuses of the files/directories in the given path if the path is
   * a directory.
   *
   * @param f given path
   * @return the statuses of the files/directories in the given patch
   * @throws FileNotFoundException when the path does not exist;
   * @throws IOException due to an IO problem.
   * @throws AmazonClientException on failures inside the AWS SDK
   */
public FileStatus[] innerListStatus(Path f) throws FileNotFoundException, IOException, AmazonClientException {
    Path path = qualify(f);
    String key = pathToKey(path);
    LOG.debug("List status for path: {}", path);
    incrementStatistic(INVOCATION_LIST_STATUS);
    List<FileStatus> result;
    final FileStatus fileStatus = getFileStatus(path);
    if (fileStatus.isDirectory()) {
        if (!key.isEmpty()) {
            key = key + '/';
        }
        ListObjectsRequest request = createListObjectsRequest(key, "/");
        LOG.debug("listStatus: doing listObjects for directory {}", key);
        Listing.FileStatusListingIterator files = listing.createFileStatusListingIterator(path, request, ACCEPT_ALL, new Listing.AcceptAllButSelfAndS3nDirs(path));
        result = new ArrayList<>(files.getBatchSize());
        while (files.hasNext()) {
            result.add(files.next());
        }
        return result.toArray(new FileStatus[result.size()]);
    } else {
        LOG.debug("Adding: rd (not a dir): {}", path);
        FileStatus[] stats = new FileStatus[1];
        stats[0] = fileStatus;
        return stats;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) FileStatus(org.apache.hadoop.fs.FileStatus) LocatedFileStatus(org.apache.hadoop.fs.LocatedFileStatus) ObjectListing(com.amazonaws.services.s3.model.ObjectListing)

Example 2 with ListObjectsRequest

use of com.amazonaws.services.s3.model.ListObjectsRequest in project hadoop by apache.

the class S3AFileSystem method innerRename.

/**
   * The inner rename operation. See {@link #rename(Path, Path)} for
   * the description of the operation.
   * This operation throws an exception on any failure which needs to be
   * reported and downgraded to a failure. That is: if a rename
   * @param src path to be renamed
   * @param dst new path after rename
   * @throws RenameFailedException if some criteria for a state changing
   * rename was not met. This means work didn't happen; it's not something
   * which is reported upstream to the FileSystem APIs, for which the semantics
   * of "false" are pretty vague.
   * @throws FileNotFoundException there's no source file.
   * @throws IOException on IO failure.
   * @throws AmazonClientException on failures inside the AWS SDK
   */
private boolean innerRename(Path src, Path dst) throws RenameFailedException, FileNotFoundException, IOException, AmazonClientException {
    LOG.debug("Rename path {} to {}", src, dst);
    incrementStatistic(INVOCATION_RENAME);
    String srcKey = pathToKey(src);
    String dstKey = pathToKey(dst);
    if (srcKey.isEmpty()) {
        throw new RenameFailedException(src, dst, "source is root directory");
    }
    if (dstKey.isEmpty()) {
        throw new RenameFailedException(src, dst, "dest is root directory");
    }
    // get the source file status; this raises a FNFE if there is no source
    // file.
    S3AFileStatus srcStatus = getFileStatus(src);
    if (srcKey.equals(dstKey)) {
        LOG.debug("rename: src and dest refer to the same file or directory: {}", dst);
        throw new RenameFailedException(src, dst, "source and dest refer to the same file or directory").withExitCode(srcStatus.isFile());
    }
    S3AFileStatus dstStatus = null;
    try {
        dstStatus = getFileStatus(dst);
        // whether or not it can be the destination of the rename.
        if (srcStatus.isDirectory()) {
            if (dstStatus.isFile()) {
                throw new RenameFailedException(src, dst, "source is a directory and dest is a file").withExitCode(srcStatus.isFile());
            } else if (!dstStatus.isEmptyDirectory()) {
                throw new RenameFailedException(src, dst, "Destination is a non-empty directory").withExitCode(false);
            }
        // at this point the destination is an empty directory
        } else {
            // empty or not
            if (dstStatus.isFile()) {
                throw new RenameFailedException(src, dst, "Cannot rename onto an existing file").withExitCode(false);
            }
        }
    } catch (FileNotFoundException e) {
        LOG.debug("rename: destination path {} not found", dst);
        // Parent must exist
        Path parent = dst.getParent();
        if (!pathToKey(parent).isEmpty()) {
            try {
                S3AFileStatus dstParentStatus = getFileStatus(dst.getParent());
                if (!dstParentStatus.isDirectory()) {
                    throw new RenameFailedException(src, dst, "destination parent is not a directory");
                }
            } catch (FileNotFoundException e2) {
                throw new RenameFailedException(src, dst, "destination has no parent ");
            }
        }
    }
    // Ok! Time to start
    if (srcStatus.isFile()) {
        LOG.debug("rename: renaming file {} to {}", src, dst);
        if (dstStatus != null && dstStatus.isDirectory()) {
            String newDstKey = dstKey;
            if (!newDstKey.endsWith("/")) {
                newDstKey = newDstKey + "/";
            }
            String filename = srcKey.substring(pathToKey(src.getParent()).length() + 1);
            newDstKey = newDstKey + filename;
            copyFile(srcKey, newDstKey, srcStatus.getLen());
        } else {
            copyFile(srcKey, dstKey, srcStatus.getLen());
        }
        innerDelete(srcStatus, false);
    } else {
        LOG.debug("rename: renaming directory {} to {}", src, dst);
        // This is a directory to directory copy
        if (!dstKey.endsWith("/")) {
            dstKey = dstKey + "/";
        }
        if (!srcKey.endsWith("/")) {
            srcKey = srcKey + "/";
        }
        //Verify dest is not a child of the source directory
        if (dstKey.startsWith(srcKey)) {
            throw new RenameFailedException(srcKey, dstKey, "cannot rename a directory to a subdirectory o fitself ");
        }
        List<DeleteObjectsRequest.KeyVersion> keysToDelete = new ArrayList<>();
        if (dstStatus != null && dstStatus.isEmptyDirectory()) {
            // delete unnecessary fake directory.
            keysToDelete.add(new DeleteObjectsRequest.KeyVersion(dstKey));
        }
        ListObjectsRequest request = new ListObjectsRequest();
        request.setBucketName(bucket);
        request.setPrefix(srcKey);
        request.setMaxKeys(maxKeys);
        ObjectListing objects = listObjects(request);
        while (true) {
            for (S3ObjectSummary summary : objects.getObjectSummaries()) {
                keysToDelete.add(new DeleteObjectsRequest.KeyVersion(summary.getKey()));
                String newDstKey = dstKey + summary.getKey().substring(srcKey.length());
                copyFile(summary.getKey(), newDstKey, summary.getSize());
                if (keysToDelete.size() == MAX_ENTRIES_TO_DELETE) {
                    removeKeys(keysToDelete, true, false);
                }
            }
            if (objects.isTruncated()) {
                objects = continueListObjects(objects);
            } else {
                if (!keysToDelete.isEmpty()) {
                    removeKeys(keysToDelete, false, false);
                }
                break;
            }
        }
    }
    if (src.getParent() != dst.getParent()) {
        deleteUnnecessaryFakeDirectories(dst.getParent());
        createFakeDirectoryIfNecessary(src.getParent());
    }
    return true;
}
Also used : Path(org.apache.hadoop.fs.Path) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest) ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest)

Example 3 with ListObjectsRequest

use of com.amazonaws.services.s3.model.ListObjectsRequest in project hadoop by apache.

the class S3AFileSystem method createListObjectsRequest.

/**
   * Create a {@code ListObjectsRequest} request against this bucket,
   * with the maximum keys returned in a query set by {@link #maxKeys}.
   * @param key key for request
   * @param delimiter any delimiter
   * @return the request
   */
private ListObjectsRequest createListObjectsRequest(String key, String delimiter) {
    ListObjectsRequest request = new ListObjectsRequest();
    request.setBucketName(bucket);
    request.setMaxKeys(maxKeys);
    request.setPrefix(key);
    if (delimiter != null) {
        request.setDelimiter(delimiter);
    }
    return request;
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest)

Example 4 with ListObjectsRequest

use of com.amazonaws.services.s3.model.ListObjectsRequest in project zeppelin by apache.

the class S3NotebookRepo method list.

@Override
public List<NoteInfo> list(AuthenticationInfo subject) throws IOException {
    List<NoteInfo> infos = new LinkedList<>();
    NoteInfo info;
    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("note.json")) {
                    info = getNoteInfo(objectSummary.getKey());
                    if (info != null) {
                        infos.add(info);
                    }
                }
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (AmazonClientException ace) {
        throw new IOException("Unable to list objects in S3: " + ace, ace);
    }
    return infos;
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) NoteInfo(org.apache.zeppelin.notebook.NoteInfo) AmazonClientException(com.amazonaws.AmazonClientException) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) IOException(java.io.IOException) LinkedList(java.util.LinkedList)

Example 5 with ListObjectsRequest

use of com.amazonaws.services.s3.model.ListObjectsRequest in project deeplearning4j by deeplearning4j.

the class S3Downloader method keysForBucket.

/**
     * Return the keys for a bucket
     * @param bucket the bucket to get the keys for
     * @return the bucket's keys
     */
public List<String> keysForBucket(String bucket) {
    AmazonS3 s3 = getClient();
    List<String> ret = new ArrayList<>();
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket);
    ObjectListing objectListing;
    do {
        objectListing = s3.listObjects(listObjectsRequest);
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            ret.add(objectSummary.getKey());
        }
        listObjectsRequest.setMarker(objectListing.getNextMarker());
    } while (objectListing.isTruncated());
    return ret;
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ArrayList(java.util.ArrayList)

Aggregations

ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)18 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)17 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)13 ArrayList (java.util.ArrayList)10 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)4 Path (org.apache.hadoop.fs.Path)4 AmazonClientException (com.amazonaws.AmazonClientException)3 AmazonServiceException (com.amazonaws.AmazonServiceException)3 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)3 IOException (java.io.IOException)3 S3Object (com.amazonaws.services.s3.model.S3Object)2 FileNotFoundException (java.io.FileNotFoundException)2 Map (java.util.Map)2 DataRecord (org.apache.jackrabbit.core.data.DataRecord)2 AerospikeHandler (com.aerospike.redisson.AerospikeHandler)1 RedissonClient (com.aerospike.redisson.RedissonClient)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1