Search in sources :

Example 36 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project druid by druid-io.

the class StaticS3FirehoseFactory method openObjectStream.

@Override
protected InputStream openObjectStream(URI object) throws IOException {
    try {
        // Get data of the given object and open an input stream
        final String bucket = object.getAuthority();
        final String key = S3Utils.extractS3Key(object);
        final S3Object s3Object = s3Client.getObject(bucket, key);
        if (s3Object == null) {
            throw new ISE("Failed to get an s3 object for bucket[%s] and key[%s]", bucket, key);
        }
        return s3Object.getObjectContent();
    } catch (AmazonS3Exception e) {
        throw new IOException(e);
    }
}
Also used : ISE(org.apache.druid.java.util.common.ISE) S3Object(com.amazonaws.services.s3.model.S3Object) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException)

Example 37 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project druid by druid-io.

the class S3InputSourceTest method expectSdkClientException.

// Setup mocks for invoquing the resettable condition for the S3Entity:
private static void expectSdkClientException(URI uri) throws IOException {
    final String s3Bucket = uri.getAuthority();
    final String key = S3Utils.extractS3Key(uri);
    S3ObjectInputStream someInputStream = EasyMock.createMock(S3ObjectInputStream.class);
    EasyMock.expect(someInputStream.read(EasyMock.anyObject(), EasyMock.anyInt(), EasyMock.anyInt())).andThrow(new SdkClientException("Data read has a different length than the expected")).anyTimes();
    someInputStream.close();
    expectLastCall().andVoid().anyTimes();
    S3Object someObject = EasyMock.createMock(S3Object.class);
    EasyMock.expect(someObject.getBucketName()).andReturn(s3Bucket).anyTimes();
    EasyMock.expect(someObject.getKey()).andReturn(key).anyTimes();
    EasyMock.expect(someObject.getObjectContent()).andReturn(someInputStream).anyTimes();
    EasyMock.expect(S3_CLIENT.getObject(EasyMock.anyObject(GetObjectRequest.class))).andReturn(someObject).anyTimes();
    EasyMock.replay(someObject);
    EasyMock.replay(someInputStream);
}
Also used : SdkClientException(com.amazonaws.SdkClientException) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) S3Object(com.amazonaws.services.s3.model.S3Object)

Example 38 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project druid by druid-io.

the class S3InputSourceTest method expectGetObject.

private static void expectGetObject(URI uri) {
    final String s3Bucket = uri.getAuthority();
    final String key = S3Utils.extractS3Key(uri);
    S3Object someObject = new S3Object();
    someObject.setBucketName(s3Bucket);
    someObject.setKey(key);
    someObject.setObjectContent(new ByteArrayInputStream(CONTENT));
    EasyMock.expect(S3_CLIENT.getObject(EasyMock.anyObject(GetObjectRequest.class))).andReturn(someObject).once();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) S3Object(com.amazonaws.services.s3.model.S3Object)

Example 39 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project druid by druid-io.

the class S3DataSegmentPullerTest method testS3ObjectStream.

@Test
public void testS3ObjectStream() throws IOException {
    final String bucket = "bucket";
    final String keyPrefix = "prefix/dir/0";
    final ServerSideEncryptingAmazonS3 s3Client = EasyMock.createStrictMock(ServerSideEncryptingAmazonS3.class);
    final byte[] value = bucket.getBytes(StandardCharsets.UTF_8);
    final File tmpFile = temporaryFolder.newFile("testObjectFile");
    try (OutputStream outputStream = new FileOutputStream(tmpFile)) {
        outputStream.write(value);
    }
    final S3Object object0 = new S3Object();
    object0.setBucketName(bucket);
    object0.setKey(keyPrefix + "/test-object");
    object0.getObjectMetadata().setLastModified(new Date(0));
    object0.setObjectContent(new FileInputStream(tmpFile));
    EasyMock.expect(s3Client.getObject(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(object0).once();
    S3DataSegmentPuller puller = new S3DataSegmentPuller(s3Client);
    EasyMock.replay(s3Client);
    InputStream stream = puller.buildFileObject(URI.create(StringUtils.format("s3://%s/%s", bucket, object0.getKey()))).openInputStream();
    EasyMock.verify(s3Client);
    Assert.assertEquals(bucket, IOUtils.toString(stream, StandardCharsets.UTF_8));
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) S3Object(com.amazonaws.services.s3.model.S3Object) File(java.io.File) Date(java.util.Date) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 40 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object 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());
}
Also used : CompletedMultipartUpload(software.amazon.awssdk.services.s3.model.CompletedMultipartUpload) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ImmutableSet(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableSet) Future(java.util.concurrent.Future) Supplier(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Supplier) Strings(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Strings) Map(java.util.Map) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) ArrayListMultimap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ArrayListMultimap) ObjectIdentifier(software.amazon.awssdk.services.s3.model.ObjectIdentifier) CompleteMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse) CopyObjectResponse(software.amazon.awssdk.services.s3.model.CopyObjectResponse) Suppliers(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Suppliers) MoreExecutors(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.MoreExecutors) Collection(java.util.Collection) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) HeadObjectRequest(software.amazon.awssdk.services.s3.model.HeadObjectRequest) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) Collectors(java.util.stream.Collectors) S3Options(org.apache.beam.sdk.io.aws2.options.S3Options) FileNotFoundException(java.io.FileNotFoundException) Executors(java.util.concurrent.Executors) ThreadFactoryBuilder(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.ThreadFactoryBuilder) Objects(java.util.Objects) List(java.util.List) DeleteObjectsRequest(software.amazon.awssdk.services.s3.model.DeleteObjectsRequest) Stream(java.util.stream.Stream) MoveOptions(org.apache.beam.sdk.io.fs.MoveOptions) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting) HeadObjectResponse(software.amazon.awssdk.services.s3.model.HeadObjectResponse) AutoValue(com.google.auto.value.AutoValue) ListeningExecutorService(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.ListeningExecutorService) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Pattern(java.util.regex.Pattern) SdkServiceException(software.amazon.awssdk.core.exception.SdkServiceException) Delete(software.amazon.awssdk.services.s3.model.Delete) FileSystem(org.apache.beam.sdk.io.FileSystem) Preconditions.checkNotNull(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull) MatchResult(org.apache.beam.sdk.io.fs.MatchResult) S3Object(software.amazon.awssdk.services.s3.model.S3Object) CopyObjectRequest(software.amazon.awssdk.services.s3.model.CopyObjectRequest) FileSystemUtils.wildcardToRegexp(org.apache.beam.sdk.io.FileSystemUtils.wildcardToRegexp) Callable(java.util.concurrent.Callable) UploadPartCopyRequest(software.amazon.awssdk.services.s3.model.UploadPartCopyRequest) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response) ArrayList(java.util.ArrayList) Multimap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Multimap) Preconditions.checkArgument(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) CreateOptions(org.apache.beam.sdk.io.fs.CreateOptions) S3Client(software.amazon.awssdk.services.s3.S3Client) IOException(java.io.IOException) CopyPartResult(software.amazon.awssdk.services.s3.model.CopyPartResult) CompletedPart(software.amazon.awssdk.services.s3.model.CompletedPart) ExecutionException(java.util.concurrent.ExecutionException) Preconditions.checkState(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState) MoreFutures(org.apache.beam.sdk.util.MoreFutures) WritableByteChannel(java.nio.channels.WritableByteChannel) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) Pattern(java.util.regex.Pattern) SdkServiceException(software.amazon.awssdk.core.exception.SdkServiceException) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) IOException(java.io.IOException) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response)

Aggregations

S3Object (com.amazonaws.services.s3.model.S3Object)76 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)22 InputStream (java.io.InputStream)22 IOException (java.io.IOException)21 S3ObjectInputStream (com.amazonaws.services.s3.model.S3ObjectInputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 Test (org.junit.Test)15 File (java.io.File)12 FileInputStream (java.io.FileInputStream)12 AmazonS3 (com.amazonaws.services.s3.AmazonS3)11 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)11 AmazonServiceException (com.amazonaws.AmazonServiceException)10 Date (java.util.Date)9 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)8 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)8 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)7 S3Object (software.amazon.awssdk.services.s3.model.S3Object)7 AmazonClientException (com.amazonaws.AmazonClientException)6 FileOutputStream (java.io.FileOutputStream)6 Test (org.testng.annotations.Test)6