software.amazon.awssdk.core.ResponseInputStream Java Examples

The following examples show how to use software.amazon.awssdk.core.ResponseInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalS3ClientTest.java    From edison-microservice with Apache License 2.0 6 votes vote down vote up
@Test
public void getObjectShouldReturnStreamWithData() throws Exception {
    // given
    testee.putObject(PutObjectRequest.builder()
                    .bucket("someBucket")
                    .key("someKey")
                    .build(),
            RequestBody.fromString("testdata"));
    //when
    ResponseInputStream<GetObjectResponse> inputStream = testee.getObject(GetObjectRequest.builder()
            .bucket("someBucket")
            .key("someKey")
            .build());

    //then
    String data = IoUtils.toUtf8String(inputStream);
    assertThat(data, is("testdata"));
}
 
Example #2
Source File: S3ObjectSerializer.java    From camel-kafka-connector with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(String topic, ResponseInputStream data) {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;
    byte[] byteArray = new byte[16384];

    try {
        while ((nRead = data.read(byteArray, 0, byteArray.length)) != -1) {
            buffer.write(byteArray, 0, nRead);
        }
    } catch (IOException e) {
        LOG.warn("I/O error while serializing data from or to topic {}: {} | {}", topic, e.getMessage(), e);
    }

    return buffer.toByteArray();
}
 
Example #3
Source File: FeatureStateConverter.java    From edison-microservice with Apache License 2.0 6 votes vote down vote up
public FeatureState retrieveFeatureStateFromS3(final Feature feature) {
    final GetObjectRequest getRequest = GetObjectRequest.builder()
            .bucket(togglzProperties.getS3().getBucketName())
            .key(keyForFeature(feature))
            .build();
    try (final ResponseInputStream<GetObjectResponse> responseStream = s3Client.getObject(getRequest)) {
        if (responseStream != null) {
            final FeatureStateStorageWrapper wrapper = objectMapper.reader()
                    .forType(FeatureStateStorageWrapper.class)
                    .readValue(responseStream);
            return FeatureStateStorageWrapper.featureStateForWrapper(feature, wrapper);
        }
    } catch (final S3Exception ae) {
        if (ERR_NO_SUCH_KEY.equals(ae.awsErrorDetails().errorCode()) ||  ae.awsErrorDetails().sdkHttpResponse().statusCode() == 404) {
            return null;
        }
        throw ae;
    } catch (final IOException e) {
        throw new RuntimeException("Failed to get the feature state", e);
    }
    return null;
}
 
Example #4
Source File: AmazonWebServicesClientProxy.java    From cloudformation-cli-java-plugin with Apache License 2.0 6 votes vote down vote up
public <RequestT extends AwsRequest, ResultT extends AwsResponse>
    ResponseInputStream<ResultT>
    injectCredentialsAndInvokeV2InputStream(final RequestT request,
                                            final Function<RequestT, ResponseInputStream<ResultT>> requestFunction) {

    AwsRequestOverrideConfiguration overrideConfiguration = AwsRequestOverrideConfiguration.builder()
        .credentialsProvider(v2CredentialsProvider).build();

    @SuppressWarnings("unchecked")
    RequestT wrappedRequest = (RequestT) request.toBuilder().overrideConfiguration(overrideConfiguration).build();

    try {
        ResponseInputStream<ResultT> response = requestFunction.apply(wrappedRequest);
        logRequestMetadataV2(request, response.response());
        return response;
    } catch (final Throwable e) {
        loggerProxy.log(String.format("Failed to execute remote function: {%s}", e.getMessage()));
        throw e;
    }
}
 
Example #5
Source File: LocalS3ClientTest.java    From synapse with Apache License 2.0 6 votes vote down vote up
@Test
public void getObjectShouldReturnStreamWithData() throws Exception {
    // given
    testee.putObject(PutObjectRequest.builder()
                    .bucket("someBucket")
                    .key("someKey")
                    .build(),
            RequestBody.fromString("testdata"));
    //when
    ResponseInputStream<GetObjectResponse> inputStream = testee.getObject(GetObjectRequest.builder()
            .bucket("someBucket")
            .key("someKey")
            .build());

    //then
    String data = IoUtils.toUtf8String(inputStream);
    assertThat(data, is("testdata"));
}
 
Example #6
Source File: S3BundlePersistenceProvider.java    From nifi-registry with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void getBundleVersionContent(final BundleVersionCoordinate versionCoordinate, final OutputStream outputStream)
        throws BundlePersistenceException {
    final String key = getKey(versionCoordinate);
    LOGGER.debug("Retrieving bundle version from S3 bucket '{}' with key '{}'", new Object[]{s3BucketName, key});

    final GetObjectRequest request = GetObjectRequest.builder()
            .bucket(s3BucketName)
            .key(key)
            .build();

    try (final ResponseInputStream<GetObjectResponse> response = s3Client.getObject(request)) {
        IoUtils.copy(response, outputStream);
        LOGGER.debug("Successfully retrieved bundle version from S3 bucket '{}' with key '{}'", new Object[]{s3BucketName, key});
    } catch (Exception e) {
        throw new BundlePersistenceException("Error retrieving bundle version from S3 due to: " + e.getMessage(), e);
    }
}
 
Example #7
Source File: SyncClientInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * @return Simple method for streaming output operations to get content as an input stream.
 */
private MethodSpec inputStreamSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) {
    TypeName returnType = ParameterizedTypeName.get(ClassName.get(ResponseInputStream.class), responseType);
    return MethodSpec.methodBuilder(opModel.getMethodName())
                     .returns(returnType)
                     .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
                     .addParameter(requestType, opModel.getInput().getVariableName())
                     .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.INPUT_STREAM))
                     .addExceptions(getExceptionClasses(model, opModel))
                     .addStatement("return $L($L, $T.toInputStream())", opModel.getMethodName(),
                                   opModel.getInput().getVariableName(),
                                   ClassName.get(ResponseTransformer.class))
                     .build();
}
 
Example #8
Source File: LocalS3Client.java    From edison-microservice with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public ResponseInputStream<GetObjectResponse> getObject(final GetObjectRequest getObjectRequest) throws S3Exception {
    final Map<String, BucketItem> bucketItemMap = bucketsWithContents.get(getObjectRequest.bucket());
    final BucketItem bucketItem = bucketItemMap.get(getObjectRequest.key());
    if (bucketItem != null) {
        try {
            return new ResponseInputStream<>(GetObjectResponse.builder().build(), toAbortableInputStream(bucketItem));
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
            throw SdkClientException.create("", e);
        }
    } else {
        throw NoSuchKeyException.builder().message("NoSuchKey").awsErrorDetails(AwsErrorDetails.builder().errorCode("NoSuchKey").build()).build();
    }
}
 
Example #9
Source File: PollyDemo.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static InputStream synthesize(PollyClient polly, String text, Voice voice, OutputFormat format) throws IOException {

        SynthesizeSpeechRequest synthReq = SynthesizeSpeechRequest.builder()
                .text(text)
                .voiceId(voice.id())
                .outputFormat(format)
                .build();

        ResponseInputStream<SynthesizeSpeechResponse> synthRes = polly.synthesizeSpeech(synthReq);
        return synthRes;
    }
 
Example #10
Source File: LocalS3Client.java    From synapse with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public ResponseInputStream<GetObjectResponse> getObject(final GetObjectRequest getObjectRequest) throws S3Exception {
    final Map<String, BucketItem> bucketItemMap = bucketsWithContents.get(getObjectRequest.bucket());
    final BucketItem bucketItem = bucketItemMap.get(getObjectRequest.key());
    try {
        return new ResponseInputStream<>(GetObjectResponse.builder().build(), toAbortableInputStream(bucketItem));
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
        throw SdkClientException.create("", e);
    }

}
 
Example #11
Source File: InMemCompactionAcceptanceTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private LinkedHashMap<String, JSONArray> fetchAndParseSnapshotFileFromS3(String snapshotFileName) {
    GetObjectRequest request = GetObjectRequest.builder().bucket(INTEGRATION_TEST_BUCKET).key(snapshotFileName).build();

    ResponseInputStream<GetObjectResponse> responseInputStream = s3Client.getObject(request);
    try (
            BufferedInputStream bufferedInputStream = new BufferedInputStream(responseInputStream);
            ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream)
    ) {
        zipInputStream.getNextEntry();
        return (LinkedHashMap<String, JSONArray>) Configuration.defaultConfiguration().jsonProvider().parse(zipInputStream, Charsets.UTF_8.name());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #12
Source File: KinesisCompactionAcceptanceTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private LinkedHashMap<String, JSONArray> fetchAndParseSnapshotFileFromS3(String snapshotFileName) {
    GetObjectRequest request = GetObjectRequest.builder().bucket(INTEGRATION_TEST_BUCKET).key(snapshotFileName).build();

    ResponseInputStream<GetObjectResponse> responseInputStream = s3Client.getObject(request);
    try (
            BufferedInputStream bufferedInputStream = new BufferedInputStream(responseInputStream);
            ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream)
    ) {
        zipInputStream.getNextEntry();
        return (LinkedHashMap<String, JSONArray>) Configuration.defaultConfiguration().jsonProvider().parse(zipInputStream, Charsets.UTF_8.name());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #13
Source File: SyncClientConnectionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void streamingOut_toInputStream_closeResponseStreamShouldCloseUnderlyingStream() throws IOException {
    ClosableStringInputStream inputStream = new ClosableStringInputStream("{}");
    mockHttpClient.stubNextResponse(mockResponse(inputStream, 200));

    ResponseInputStream<StreamingOutputOperationResponse> responseInputStream =
        client.streamingOutputOperation(b -> b.build(), ResponseTransformer.toInputStream());

    assertThat(inputStream.isClosed()).isFalse();
    responseInputStream.close();
    assertThat(inputStream.isClosed()).isTrue();
}
 
Example #14
Source File: GetObjectIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void toInputStream_loadFromProperties() throws IOException {
    s3.putObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY), RequestBody.fromString("test: test"));
    try (ResponseInputStream<GetObjectResponse> object = s3.getObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY),
                                                                      ResponseTransformer.toInputStream())) {
        Properties properties = new Properties();
        properties.load(object);
        assertThat(properties.getProperty("test")).isEqualTo("test");
    }
}
 
Example #15
Source File: AmazonWebServicesClientProxyTest.java    From cloudformation-cli-java-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testInjectCredentialsAndInvokeV2InputStream() {

    final LoggerProxy loggerProxy = mock(LoggerProxy.class);
    final Credentials credentials = new Credentials("accessKeyId", "secretAccessKey", "sessionToken");
    final ResponseInputStream<?> responseInputStream = mock(ResponseInputStream.class);

    final AmazonWebServicesClientProxy proxy = new AmazonWebServicesClientProxy(loggerProxy, credentials, () -> 1000L);

    final software.amazon.awssdk.services.s3.model.GetObjectRequest wrappedRequest = mock(
        software.amazon.awssdk.services.s3.model.GetObjectRequest.class);

    final software.amazon.awssdk.services.s3.model.GetObjectRequest.Builder builder = mock(
        software.amazon.awssdk.services.s3.model.GetObjectRequest.Builder.class);
    when(builder.overrideConfiguration(any(AwsRequestOverrideConfiguration.class))).thenReturn(builder);
    when(builder.build()).thenReturn(wrappedRequest);
    final software.amazon.awssdk.services.s3.model.GetObjectRequest request = mock(
        software.amazon.awssdk.services.s3.model.GetObjectRequest.class);
    when(request.toBuilder()).thenReturn(builder);

    final S3Client client = mock(S3Client.class);

    doReturn(responseInputStream).when(client)
        .getObject(any(software.amazon.awssdk.services.s3.model.GetObjectRequest.class));

    final ResponseInputStream<
        GetObjectResponse> result = proxy.injectCredentialsAndInvokeV2InputStream(request, client::getObject);

    // verify request is rebuilt for injection
    verify(request).toBuilder();

    // verify the wrapped request is sent over the initiate
    verify(client).getObject(wrappedRequest);

    // ensure the return type matches
    assertThat(result).isEqualTo(responseInputStream);
}
 
Example #16
Source File: S3Repository.java    From djl with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void download(Path tmp, URI baseUri, Artifact.Item item, Progress progress)
        throws IOException {
    String key = item.getUri();
    logger.debug("Downloading artifact from: s3://{}/{} ...", bucket, key);
    GetObjectRequest req = GetObjectRequest.builder().bucket(bucket).key(key).build();
    try (ResponseInputStream<GetObjectResponse> is = client.getObject(req)) {
        save(is, tmp, baseUri, item, progress);
    }
}
 
Example #17
Source File: S3ObjectTransforms.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
@Override
public R apply(R record) {
    byte[] v = serializer.serialize(record.topic(), (ResponseInputStream)record.value());
    String finalValue = new String(v);
    return record.newRecord(record.topic(), record.kafkaPartition(), null, record.key(), Schema.STRING_SCHEMA, finalValue, record.timestamp());
}
 
Example #18
Source File: BaseTimeoutTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public ResponseInputStream<ResponseT> transform(ResponseT response, AbortableInputStream inputStream) throws Exception {
    wastingTimeInterruptibly();
    return delegate.transform(response, inputStream);
}
 
Example #19
Source File: GetObjectIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void contentRangeIsReturnedForRangeRequests() {
    ResponseInputStream<GetObjectResponse> stream = s3.getObject(getObjectRequest.copy(r -> r.range("bytes=0-1")));
    stream.abort();
    assertThat(stream.response().contentRange()).isEqualTo("bytes 0-1/10000");
}
 
Example #20
Source File: GetObjectIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void toInputStream() throws Exception {
    try (ResponseInputStream<GetObjectResponse> content = s3.getObject(getObjectRequest)) {
    }
}
 
Example #21
Source File: UploadLargeObjectIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private void verifyResponse(String key) {
    ResponseInputStream<GetObjectResponse> responseInputStream = s3.getObject(b -> b.bucket(BUCKET).key(key));
    IoUtils.drainInputStream(responseInputStream);
}
 
Example #22
Source File: S3ObjectConverter.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] fromConnectData(String topic, Schema schema, Object value) {
    return serializer.serialize(topic, (ResponseInputStream)value);
}
 
Example #23
Source File: test-json-client-interface.java    From aws-sdk-java-v2 with Apache License 2.0 3 votes vote down vote up
/**
 * Some operation with a streaming output<br/>
 * <p>
 * This is a convenience which creates an instance of the {@link StreamingOutputOperationRequest.Builder} avoiding
 * the need to create one manually via {@link StreamingOutputOperationRequest#builder()}
 * </p>
 *
 * @param streamingOutputOperationRequest
 *        A {@link Consumer} that will call methods on {@link StreamingOutputOperationRequest.Builder} to create a
 *        request.
 * @return A {@link ResponseInputStream} containing data streamed from service. Note that this is an unmanaged
 *         reference to the underlying HTTP connection so great care must be taken to ensure all data if fully read
 *         from the input stream and that it is properly closed. Failure to do so may result in sub-optimal behavior
 *         and exhausting connections in the connection pool. The unmarshalled response object can be obtained via
 *         {@link ResponseInputStream#response()}. The service documentation for the response content is as follows
 *         'This be a stream'.
 * @throws SdkException
 *         Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
 *         catch all scenarios.
 * @throws SdkClientException
 *         If any client side error occurs such as an IO related failure, failure to get credentials, etc.
 * @throws JsonException
 *         Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
 * @sample JsonClient.StreamingOutputOperation
 * @see #getObject(streamingOutputOperation, ResponseTransformer)
 * @see <a href="http://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/StreamingOutputOperation"
 *      target="_top">AWS API Documentation</a>
 */
default ResponseInputStream<StreamingOutputOperationResponse> streamingOutputOperation(
        Consumer<StreamingOutputOperationRequest.Builder> streamingOutputOperationRequest) throws AwsServiceException,
                                                                                                  SdkClientException, JsonException {
    return streamingOutputOperation(StreamingOutputOperationRequest.builder().applyMutation(streamingOutputOperationRequest)
                                                                   .build());
}
 
Example #24
Source File: ProxyClient.java    From cloudformation-cli-java-plugin with Apache License 2.0 3 votes vote down vote up
/**
 * This is a synchronous version of making API calls which implement
 * ResponseInputStream in the SDKv2
 *
 * @param request, the AWS service request that we need to make
 * @param requestFunction, this is a Lambda closure that provide the actual API
 *            that needs to be invoked.
 * @param <RequestT> the request type
 * @param <ResponseT> the response from the request
 * @return the response if successful. Else it will propagate all
 *         {@link software.amazon.awssdk.awscore.exception.AwsServiceException}
 *         that is thrown or
 *         {@link software.amazon.awssdk.core.exception.SdkClientException} if
 *         there is client side problem
 */
default <RequestT extends AwsRequest, ResponseT extends AwsResponse>
    ResponseInputStream<ResponseT>
    injectCredentialsAndInvokeV2InputStream(RequestT request,
                                            Function<RequestT, ResponseInputStream<ResponseT>> requestFunction) {
    throw new UnsupportedOperationException();
}
 
Example #25
Source File: test-json-client-interface.java    From aws-sdk-java-v2 with Apache License 2.0 2 votes vote down vote up
/**
 * Some operation with a streaming output
 *
 * @param streamingOutputOperationRequest
 * @return A {@link ResponseInputStream} containing data streamed from service. Note that this is an unmanaged
 *         reference to the underlying HTTP connection so great care must be taken to ensure all data if fully read
 *         from the input stream and that it is properly closed. Failure to do so may result in sub-optimal behavior
 *         and exhausting connections in the connection pool. The unmarshalled response object can be obtained via
 *         {@link ResponseInputStream#response()}. The service documentation for the response content is as follows
 *         'This be a stream'.
 * @throws SdkException
 *         Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
 *         catch all scenarios.
 * @throws SdkClientException
 *         If any client side error occurs such as an IO related failure, failure to get credentials, etc.
 * @throws JsonException
 *         Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
 * @sample JsonClient.StreamingOutputOperation
 * @see #getObject(streamingOutputOperation, ResponseTransformer)
 * @see <a href="http://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/StreamingOutputOperation"
 *      target="_top">AWS API Documentation</a>
 */
default ResponseInputStream<StreamingOutputOperationResponse> streamingOutputOperation(
        StreamingOutputOperationRequest streamingOutputOperationRequest) throws AwsServiceException, SdkClientException,
                                                                                JsonException {
    return streamingOutputOperation(streamingOutputOperationRequest, ResponseTransformer.toInputStream());
}
 
Example #26
Source File: ResponseTransformer.java    From aws-sdk-java-v2 with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a response transformer that returns an unmanaged input stream with the response content. This input stream must
 * be explicitly closed to release the connection. The unmarshalled response object can be obtained via the {@link
 * ResponseInputStream#response} method.
 * <p>
 * Note that the returned stream is not subject to the retry policy or timeout settings (except for socket timeout)
 * of the client. No retries will be performed in the event of a socket read failure or connection reset.
 *
 * @param <ResponseT> Type of unmarshalled response POJO.
 * @return ResponseTransformer instance.
 */
static <ResponseT> ResponseTransformer<ResponseT, ResponseInputStream<ResponseT>> toInputStream() {
    return unmanaged(ResponseInputStream::new);
}