use of software.amazon.awssdk.core.exception.SdkServiceException in project beam by apache.
the class S3FileSystem method expandGlob.
private ExpandedGlob expandGlob(S3ResourceId glob) {
// The S3 API can list objects, filtered by prefix, but not by wildcard.
// Here, we find the longest prefix without wildcard "*",
// then filter the results with a regex.
checkArgument(glob.isWildcard(), "isWildcard");
String keyPrefix = glob.getKeyNonWildcardPrefix();
Pattern wildcardRegexp = Pattern.compile(wildcardToRegexp(glob.getKey()));
LOG.debug("expanding bucket {}, prefix {}, against pattern {}", glob.getBucket(), keyPrefix, wildcardRegexp);
ImmutableList.Builder<S3ResourceId> expandedPaths = ImmutableList.builder();
String continuationToken = null;
do {
ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(glob.getBucket()).prefix(keyPrefix).continuationToken(continuationToken).build();
ListObjectsV2Response response;
try {
response = s3Client.get().listObjectsV2(request);
} catch (SdkServiceException e) {
return ExpandedGlob.create(glob, new IOException(e));
}
continuationToken = response.nextContinuationToken();
List<S3Object> contents = response.contents();
contents.stream().filter(s3Object -> wildcardRegexp.matcher(s3Object.key()).matches()).forEach(s3Object -> {
S3ResourceId expandedPath = S3ResourceId.fromComponents(glob.getScheme(), glob.getBucket(), s3Object.key()).withSize(s3Object.size()).withLastModified(Date.from(s3Object.lastModified()));
LOG.debug("Expanded S3 object path {}", expandedPath);
expandedPaths.add(expandedPath);
});
} while (continuationToken != null);
return ExpandedGlob.create(glob, expandedPaths.build());
}
use of software.amazon.awssdk.core.exception.SdkServiceException in project beam by apache.
the class S3FileSystem method delete.
private void delete(String bucket, Collection<String> keys) throws IOException {
checkArgument(keys.size() <= MAX_DELETE_OBJECTS_PER_REQUEST, "only %s keys can be deleted per request, but got %s", MAX_DELETE_OBJECTS_PER_REQUEST, keys.size());
List<ObjectIdentifier> deleteKeyVersions = keys.stream().map((key) -> ObjectIdentifier.builder().key(key).build()).collect(Collectors.toList());
Delete delete = Delete.builder().objects(deleteKeyVersions).quiet(true).build();
DeleteObjectsRequest deleteObjectsRequest = DeleteObjectsRequest.builder().bucket(bucket).delete(delete).build();
try {
s3Client.get().deleteObjects(deleteObjectsRequest);
} catch (SdkServiceException e) {
throw new IOException(e);
}
}
use of software.amazon.awssdk.core.exception.SdkServiceException in project beam by apache.
the class S3ReadableSeekableByteChannel method read.
@Override
public int read(ByteBuffer destinationBuffer) throws IOException {
if (!isOpen()) {
throw new ClosedChannelException();
}
if (!destinationBuffer.hasRemaining()) {
return 0;
}
if (position == contentLength) {
return -1;
}
if (s3ResponseInputStream == null) {
GetObjectRequest.Builder builder = GetObjectRequest.builder().bucket(path.getBucket()).key(path.getKey()).sseCustomerKey(config.getSSECustomerKey().getKey()).sseCustomerAlgorithm(config.getSSECustomerKey().getAlgorithm());
if (position > 0) {
builder.range(String.format("bytes=%s-%s", position, contentLength));
}
GetObjectRequest request = builder.build();
try {
s3ResponseInputStream = s3Client.getObject(request);
} catch (SdkClientException e) {
throw new IOException(e);
}
s3ObjectContentChannel = Channels.newChannel(new BufferedInputStream(s3ResponseInputStream, 1024 * 1024));
}
int totalBytesRead = 0;
int bytesRead = 0;
do {
totalBytesRead += bytesRead;
try {
bytesRead = s3ObjectContentChannel.read(destinationBuffer);
} catch (SdkServiceException e) {
throw new IOException(e);
}
} while (bytesRead > 0);
position += totalBytesRead;
return totalBytesRead;
}
use of software.amazon.awssdk.core.exception.SdkServiceException in project beam by apache.
the class S3FileSystemTest method matchNonGlobNotFound.
@Test
public void matchNonGlobNotFound() {
S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Config("s3"));
S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/testdirectory/nonexistentfile");
SdkServiceException exception = S3Exception.builder().message("mock exception").statusCode(404).build();
when(s3FileSystem.getS3Client().headObject(argThat(new GetHeadObjectRequestMatcher(HeadObjectRequest.builder().bucket(path.getBucket()).key(path.getKey()).build())))).thenThrow(exception);
MatchResult result = s3FileSystem.matchNonGlobPath(path);
assertThat(result, MatchResultMatcher.create(MatchResult.Status.NOT_FOUND, new FileNotFoundException()));
}
use of software.amazon.awssdk.core.exception.SdkServiceException in project beam by apache.
the class S3FileSystemTest method matchNonGlobForbidden.
@Test
public void matchNonGlobForbidden() {
S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Config("s3"));
SdkServiceException exception = S3Exception.builder().message("mock exception").statusCode(403).build();
S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/testdirectory/keyname");
when(s3FileSystem.getS3Client().headObject(argThat(new GetHeadObjectRequestMatcher(HeadObjectRequest.builder().bucket(path.getBucket()).key(path.getKey()).build())))).thenThrow(exception);
assertThat(s3FileSystem.matchNonGlobPath(path), MatchResultMatcher.create(MatchResult.Status.ERROR, new IOException(exception)));
}
Aggregations