Search in sources :

Example 31 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project presto by prestodb.

the class TestPrestoS3FileSystem method testPrestoS3InputStreamEOS.

@Test
public void testPrestoS3InputStreamEOS() throws Exception {
    try (PrestoS3FileSystem fs = new PrestoS3FileSystem()) {
        AtomicInteger readableBytes = new AtomicInteger(1);
        MockAmazonS3 s3 = new MockAmazonS3() {

            @Override
            public S3Object getObject(GetObjectRequest req) {
                return new S3Object() {

                    @Override
                    public S3ObjectInputStream getObjectContent() {
                        return new S3ObjectInputStream(new ByteArrayInputStream(new byte[readableBytes.get()]), null);
                    }
                };
            }
        };
        fs.initialize(new URI("s3n://test-bucket/"), new Configuration());
        fs.setS3Client(s3);
        try (FSDataInputStream inputStream = fs.open(new Path("s3n://test-bucket/test"))) {
            assertEquals(inputStream.read(0, new byte[2], 0, 2), 1);
            readableBytes.set(0);
            assertEquals(inputStream.read(0, new byte[1], 0, 1), -1);
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) ClientConfiguration(com.amazonaws.ClientConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) S3ObjectInputStream(com.amazonaws.services.s3.model.S3ObjectInputStream) S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 32 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project flink by apache.

the class AWSServicesTestUtils method listBucketObjects.

public static List<S3Object> listBucketObjects(S3AsyncClient s3, String bucketName) throws ExecutionException, InterruptedException {
    ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
    CompletableFuture<ListObjectsResponse> res = s3.listObjects(listObjects);
    return res.get().contents();
}
Also used : ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse)

Example 33 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project crate by crate.

the class S3FileReadingCollectorTest method createBatchIterator.

private BatchIterator<Row> createBatchIterator(Collection<String> fileUris, String compression, final S3ObjectInputStream s3InputStream, boolean collectSourceUriFailure) {
    InputFactory.Context<LineCollectorExpression<?>> ctx = inputFactory.ctxForRefs(txnCtx, FileLineReferenceResolver::getImplementation);
    List<Input<?>> inputs = new ArrayList<>(2);
    Reference raw = createReference(SourceLineExpression.COLUMN_NAME, DataTypes.STRING);
    inputs.add(ctx.add(raw));
    if (collectSourceUriFailure) {
        Reference sourceUriFailure = createReference(SourceUriFailureExpression.COLUMN_NAME, DataTypes.STRING);
        // noinspection unchecked
        sourceUriFailureInput = (Input<String>) ctx.add(sourceUriFailure);
        inputs.add(sourceUriFailureInput);
    }
    return FileReadingIterator.newInstance(fileUris, inputs, ctx.expressions(), compression, Map.of(S3FileInputFactory.NAME, new FileInputFactory() {

        @Override
        public FileInput create(URI uri, Settings withClauseOptions) throws IOException {
            return new S3FileInput(new S3ClientHelper() {

                @Override
                protected AmazonS3 initClient(String accessKey, String secretKey, String endpoint, String protocol) throws IOException {
                    AmazonS3 client = mock(AmazonS3Client.class);
                    ObjectListing objectListing = mock(ObjectListing.class);
                    S3ObjectSummary summary = mock(S3ObjectSummary.class);
                    S3Object s3Object = mock(S3Object.class);
                    when(client.listObjects(anyString(), anyString())).thenReturn(objectListing);
                    when(objectListing.getObjectSummaries()).thenReturn(Collections.singletonList(summary));
                    when(summary.getKey()).thenReturn("foo");
                    when(client.getObject("fakebucket", "foo")).thenReturn(s3Object);
                    when(s3Object.getObjectContent()).thenReturn(s3InputStream);
                    when(client.listNextBatchOfObjects(any(ObjectListing.class))).thenReturn(objectListing);
                    when(objectListing.isTruncated()).thenReturn(false);
                    return client;
                }
            }, uri, "https");
        }
    }), false, 1, 0, CopyFromParserProperties.DEFAULT, FileUriCollectPhase.InputFormat.JSON, Settings.EMPTY);
}
Also used : FileInputFactory(io.crate.execution.engine.collect.files.FileInputFactory) InputFactory(io.crate.expression.InputFactory) AmazonS3(com.amazonaws.services.s3.AmazonS3) LineCollectorExpression(io.crate.execution.engine.collect.files.LineCollectorExpression) TestingHelpers.createReference(io.crate.testing.TestingHelpers.createReference) Reference(io.crate.metadata.Reference) ArrayList(java.util.ArrayList) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) URI(java.net.URI) Input(io.crate.data.Input) FileInput(io.crate.execution.engine.collect.files.FileInput) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) FileLineReferenceResolver(io.crate.expression.reference.file.FileLineReferenceResolver) S3ClientHelper(io.crate.copy.s3.common.S3ClientHelper) FileInputFactory(io.crate.execution.engine.collect.files.FileInputFactory) S3Object(com.amazonaws.services.s3.model.S3Object) Settings(org.elasticsearch.common.settings.Settings)

Example 34 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project crate by crate.

the class S3FileInput method getStream.

@Override
public InputStream getStream(URI uri) throws IOException {
    S3URI s3URI = S3URI.toS3URI(uri);
    if (client == null) {
        client = clientBuilder.client(s3URI, protocolSetting);
    }
    S3Object object = client.getObject(s3URI.bucket(), s3URI.key());
    if (object != null) {
        return object.getObjectContent();
    }
    throw new IOException("Failed to load S3 URI: " + uri.toString());
}
Also used : S3URI(io.crate.copy.s3.common.S3URI) S3Object(com.amazonaws.services.s3.model.S3Object) IOException(java.io.IOException)

Example 35 with S3Object

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

the class S3Entity method readFrom.

@Override
protected InputStream readFrom(long offset) throws IOException {
    final GetObjectRequest request = new GetObjectRequest(object.getBucket(), object.getPath());
    request.setRange(offset);
    try {
        final S3Object s3Object = s3Client.getObject(request);
        if (s3Object == null) {
            throw new ISE("Failed to get an s3 object for bucket[%s], key[%s], and start[%d]", object.getBucket(), object.getPath(), offset);
        }
        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) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest)

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