Search in sources :

Example 1 with NOT_FOUND

use of software.amazon.awssdk.http.HttpStatusCode.NOT_FOUND 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)

Aggregations

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