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);
}
}
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);
}
}
Aggregations