Search in sources :

Example 1 with NOFOLLOW_LINKS

use of java.nio.file.LinkOption.NOFOLLOW_LINKS in project aws-java-nio-spi-for-s3 by awslabs.

the class S3FileSystemProvider method checkAccess.

/**
 * Composable and testable version of {@code checkAccess} that uses the provided client to check access
 */
protected void checkAccess(S3AsyncClient s3Client, Path path, AccessMode... modes) throws IOException, ExecutionException, InterruptedException {
    if (Arrays.asList(modes).contains(AccessMode.WRITE)) {
        throw new UnsupportedOperationException("WRITE is not currently supported. Please raise a feature request if you want this.");
    }
    assert path instanceof S3Path;
    S3Path s3Path = (S3Path) path.toRealPath(NOFOLLOW_LINKS);
    final String bucketName = s3Path.getFileSystem().bucketName();
    if (s3Client == null) {
        s3Client = S3ClientStore.getInstance().getAsyncClientForBucketName(bucketName);
    }
    final CompletableFuture<? extends S3Response> response;
    if (s3Path.equals(s3Path.getRoot())) {
        response = s3Client.headBucket(request -> request.bucket(bucketName));
    } else {
        response = s3Client.headObject(req -> req.bucket(bucketName).key(s3Path.getKey()));
    }
    long timeOut = TimeOutUtils.TIMEOUT_TIME_LENGTH_1;
    TimeUnit unit = MINUTES;
    try {
        SdkHttpResponse httpResponse = response.get(timeOut, unit).sdkHttpResponse();
        if (httpResponse.isSuccessful())
            return;
        if (httpResponse.statusCode() == FORBIDDEN)
            throw new AccessDeniedException(s3Path.toString());
        if (httpResponse.statusCode() == NOT_FOUND)
            throw new NoSuchFileException(s3Path.toString());
        throw new IOException(String.format("exception occurred while checking access, response code was '%d'", httpResponse.statusCode()));
    } catch (TimeoutException e) {
        throw logAndGenerateExceptionOnTimeOut(logger, "checkAccess", timeOut, unit);
    }
}
Also used : java.util(java.util) S3Object(software.amazon.awssdk.services.s3.model.S3Object) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) MINUTES(java.util.concurrent.TimeUnit.MINUTES) FileSystemProvider(java.nio.file.spi.FileSystemProvider) java.nio.file(java.nio.file) NOFOLLOW_LINKS(java.nio.file.LinkOption.NOFOLLOW_LINKS) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) TimeOutUtils.logAndGenerateExceptionOnTimeOut(software.amazon.nio.spi.s3.util.TimeOutUtils.logAndGenerateExceptionOnTimeOut) URI(java.net.URI) TimeOutUtils(software.amazon.nio.spi.s3.util.TimeOutUtils) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) FORBIDDEN(software.amazon.awssdk.http.HttpStatusCode.FORBIDDEN) Logger(org.slf4j.Logger) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) TIMEOUT_TIME_LENGTH_1(software.amazon.nio.spi.s3.util.TimeOutUtils.TIMEOUT_TIME_LENGTH_1) FileAttributeView(java.nio.file.attribute.FileAttributeView) IOException(java.io.IOException) FileAttribute(java.nio.file.attribute.FileAttribute) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) SeekableByteChannel(java.nio.channels.SeekableByteChannel) NOT_FOUND(software.amazon.awssdk.http.HttpStatusCode.NOT_FOUND) S3Response(software.amazon.awssdk.services.s3.model.S3Response) TimeUnit(java.util.concurrent.TimeUnit) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with NOFOLLOW_LINKS

use of java.nio.file.LinkOption.NOFOLLOW_LINKS in project aws-java-nio-spi-for-s3 by awslabs.

the class S3FileSystemProvider method newDirectoryStream.

/**
 * Get a new directory stream that will use the specified client. A composable and testable version of the public
 * version of {@code newDirectoryStream}
 */
protected DirectoryStream<Path> newDirectoryStream(S3AsyncClient s3Client, Path dir, DirectoryStream.Filter<? super Path> filter) throws ExecutionException, InterruptedException {
    S3Path s3Path = (S3Path) dir;
    if (s3Client == null) {
        s3Client = S3ClientStore.getInstance().getAsyncClientForBucketName(s3Path.bucketName());
    }
    String pathString = s3Path.toRealPath(NOFOLLOW_LINKS).toString();
    if (!pathString.endsWith(S3Path.PATH_SEPARATOR) && !pathString.isEmpty()) {
        pathString = pathString + S3Path.PATH_SEPARATOR;
    }
    final String bucketName = s3Path.bucketName();
    final S3FileSystem fs = new S3FileSystem(bucketName);
    final String prefix = pathString;
    long timeOut = TIMEOUT_TIME_LENGTH_1;
    final TimeUnit unit = MINUTES;
    try {
        final Iterator<S3Path> filteredDirectoryContents = s3Client.listObjectsV2(req -> req.bucket(bucketName).prefix(prefix)).get(timeOut, unit).contents().stream().map(s3Object -> truncateByPrefix(fs, prefix, s3Object)).filter(path -> {
            try {
                return filter.accept(path);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }).iterator();
        return new DirectoryStream<Path>() {

            final Iterator<? extends Path> iterator = filteredDirectoryContents;

            @Override
            @SuppressWarnings("unchecked")
            public Iterator<Path> iterator() {
                return (Iterator<Path>) iterator;
            }

            @Override
            public void close() {
            // nothing to close
            }
        };
    } catch (TimeoutException e) {
        throw logAndGenerateExceptionOnTimeOut(logger, "newDirectoryStream", timeOut, unit);
    }
}
Also used : java.util(java.util) S3Object(software.amazon.awssdk.services.s3.model.S3Object) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) MINUTES(java.util.concurrent.TimeUnit.MINUTES) FileSystemProvider(java.nio.file.spi.FileSystemProvider) java.nio.file(java.nio.file) NOFOLLOW_LINKS(java.nio.file.LinkOption.NOFOLLOW_LINKS) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) TimeOutUtils.logAndGenerateExceptionOnTimeOut(software.amazon.nio.spi.s3.util.TimeOutUtils.logAndGenerateExceptionOnTimeOut) URI(java.net.URI) TimeOutUtils(software.amazon.nio.spi.s3.util.TimeOutUtils) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) FORBIDDEN(software.amazon.awssdk.http.HttpStatusCode.FORBIDDEN) Logger(org.slf4j.Logger) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) TIMEOUT_TIME_LENGTH_1(software.amazon.nio.spi.s3.util.TimeOutUtils.TIMEOUT_TIME_LENGTH_1) FileAttributeView(java.nio.file.attribute.FileAttributeView) IOException(java.io.IOException) FileAttribute(java.nio.file.attribute.FileAttribute) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) SeekableByteChannel(java.nio.channels.SeekableByteChannel) NOT_FOUND(software.amazon.awssdk.http.HttpStatusCode.NOT_FOUND) S3Response(software.amazon.awssdk.services.s3.model.S3Response) TimeUnit(java.util.concurrent.TimeUnit) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

IOException (java.io.IOException)2 URI (java.net.URI)2 SeekableByteChannel (java.nio.channels.SeekableByteChannel)2 java.nio.file (java.nio.file)2 NOFOLLOW_LINKS (java.nio.file.LinkOption.NOFOLLOW_LINKS)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 FileAttribute (java.nio.file.attribute.FileAttribute)2 FileAttributeView (java.nio.file.attribute.FileAttributeView)2 FileSystemProvider (java.nio.file.spi.FileSystemProvider)2 java.util (java.util)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2 MINUTES (java.util.concurrent.TimeUnit.MINUTES)2 TimeoutException (java.util.concurrent.TimeoutException)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 FORBIDDEN (software.amazon.awssdk.http.HttpStatusCode.FORBIDDEN)2