Search in sources :

Example 1 with WriteGetObjectResponseRequest

use of software.amazon.awssdk.services.s3.model.WriteGetObjectResponseRequest in project amazon-s3-object-lambda-default-configuration by aws-samples.

the class GetObjectResponseHandler method writeS3GetObjectErrorResponse.

public void writeS3GetObjectErrorResponse(HttpResponse<InputStream> presignedResponse) {
    S3RequestError s3errorResponse;
    try {
        var xmlResponse = IOUtils.toString(presignedResponse.body());
        s3errorResponse = new XMLErrorParser().parse(xmlResponse);
    } catch (IOException | ParserConfigurationException | SAXException | NullPointerException e) {
        this.logger.error("Error while reading the S3 error response body: " + e);
        writeErrorResponse("Unexpected error while reading the S3 error response", Error.SERVER_ERROR);
        return;
    }
    this.s3Client.writeGetObjectResponse(new WriteGetObjectResponseRequest().withRequestRoute(this.event.outputRoute()).withRequestToken(this.event.outputToken()).withErrorCode(s3errorResponse.getCode()).withContentLength(0L).withInputStream(new ByteArrayInputStream(new byte[0])).withErrorMessage(s3errorResponse.getMessage()).withStatusCode(presignedResponse.statusCode()));
}
Also used : WriteGetObjectResponseRequest(com.amazonaws.services.s3.model.WriteGetObjectResponseRequest) S3RequestError(com.example.s3objectlambda.error.S3RequestError) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLErrorParser(com.example.s3objectlambda.error.XMLErrorParser) SAXException(org.xml.sax.SAXException)

Example 2 with WriteGetObjectResponseRequest

use of software.amazon.awssdk.services.s3.model.WriteGetObjectResponseRequest in project amazon-s3-object-lambda-default-configuration by aws-samples.

the class GetObjectResponseHandler method writeObjectResponse.

public void writeObjectResponse(HttpResponse<InputStream> presignedResponse, byte[] responseObjectByteArray) {
    Checksum checksum;
    try {
        checksum = this.checksumGenerator.getChecksum(responseObjectByteArray);
    } catch (Exception e) {
        this.logger.error("Error while writing object response" + e);
        writeErrorResponse("Error while writing object response.", Error.SERVER_ERROR);
        return;
    }
    var checksumMap = new HashMap<String, String>();
    checksumMap.put("algorithm", checksum.getAlgorithm());
    checksumMap.put("digest", checksum.getChecksum());
    var checksumObjectMetaData = new ObjectMetadata();
    checksumObjectMetaData.setUserMetadata(checksumMap);
    this.s3Client.writeGetObjectResponse(new WriteGetObjectResponseRequest().withRequestRoute(event.outputRoute()).withRequestToken(event.outputToken()).withInputStream(new ByteArrayInputStream(responseObjectByteArray)).withMetadata(checksumObjectMetaData).withStatusCode(presignedResponse.statusCode()));
}
Also used : WriteGetObjectResponseRequest(com.amazonaws.services.s3.model.WriteGetObjectResponseRequest) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) Checksum(com.example.s3objectlambda.checksum.Checksum) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 3 with WriteGetObjectResponseRequest

use of software.amazon.awssdk.services.s3.model.WriteGetObjectResponseRequest in project aws-sdk-java-v2 by aws.

the class S3ObjectLambdaOperationEndpointResolverTest method writeGetObjectResponse_regionIsS3External_shouldConvertEndpoint.

@Test
// TODO: Taken from the SEP but this test case is suspect. The SEP
@Disabled
public // endpoint s3.s3-external-1.amazonaws.com.
void writeGetObjectResponse_regionIsS3External_shouldConvertEndpoint() {
    String requestRoute = "route";
    String region = "s3-external-1";
    String expectedHost = "route.s3-object-lambda.us-east-1.amazonaws.com";
    WriteGetObjectResponseRequest request = WriteGetObjectResponseRequest.builder().requestRoute(requestRoute).build();
    verifyObjectLambdaEndpoint("https", request, region, null, URI.create("https://" + expectedHost), S3Configuration.builder());
    verifyObjectLambdaEndpoint("http", request, region, null, URI.create("http://" + expectedHost), S3Configuration.builder());
}
Also used : WriteGetObjectResponseRequest(software.amazon.awssdk.services.s3.model.WriteGetObjectResponseRequest) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 4 with WriteGetObjectResponseRequest

use of software.amazon.awssdk.services.s3.model.WriteGetObjectResponseRequest in project aws-sdk-java-v2 by aws.

the class S3ObjectLambdaOperationEndpointResolverTest method writeGetObjectResponse_regionIsFipsInSuffix_shouldConvertEndpoint.

@Test
// SDK doesn't resolve fips endpoints correctly
@Disabled
public void writeGetObjectResponse_regionIsFipsInSuffix_shouldConvertEndpoint() {
    String requestRoute = "route";
    String region = "us-gov-east-1-fips";
    String expectedHost = "route.s3-object-lambda-fips.us-gov-east-1.amazonaws.com";
    WriteGetObjectResponseRequest request = WriteGetObjectResponseRequest.builder().requestRoute(requestRoute).build();
    verifyObjectLambdaEndpoint("https", request, region, null, URI.create("https://" + expectedHost), S3Configuration.builder());
    verifyObjectLambdaEndpoint("http", request, region, null, URI.create("http://" + expectedHost), S3Configuration.builder());
}
Also used : WriteGetObjectResponseRequest(software.amazon.awssdk.services.s3.model.WriteGetObjectResponseRequest) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 5 with WriteGetObjectResponseRequest

use of software.amazon.awssdk.services.s3.model.WriteGetObjectResponseRequest in project aws-sdk-java-v2 by aws.

the class S3ObjectLambdaOperationEndpointResolverTest method verifyObjectLambdaEndpoint.

private void verifyObjectLambdaEndpoint(String protocol, S3Request request, String region, URI endpointOverride, URI expectedEndpoint, S3Configuration.Builder serviceConfigurationBuilder) {
    URI defaultRequestEndpoint = ServiceMetadata.of("s3").endpointFor(Region.of(region));
    String prefix = "";
    if (request instanceof WriteGetObjectResponseRequest) {
        prefix = ((WriteGetObjectResponseRequest) request).requestRoute() + ".";
    }
    defaultRequestEndpoint = URI.create(protocol + "://" + prefix + defaultRequestEndpoint.toString());
    SdkHttpRequest httpRequest;
    if (endpointOverride != null) {
        endpointOverride = URI.create(protocol + "://" + prefix + endpointOverride.getHost());
        httpRequest = InterceptorTestUtils.sdkHttpRequest(endpointOverride);
    } else {
        httpRequest = InterceptorTestUtils.sdkHttpRequest(defaultRequestEndpoint);
    }
    S3EndpointResolverContext context = S3EndpointResolverContext.builder().endpointOverride(endpointOverride).originalRequest(request).region(Region.of(region)).serviceConfiguration(serviceConfigurationBuilder.build()).request(httpRequest).build();
    ConfiguredS3SdkHttpRequest configuredS3SdkHttpRequest = endpointResolver.applyEndpointConfiguration(context);
    assertThat(configuredS3SdkHttpRequest.signingServiceModification().get()).isEqualTo("s3-object-lambda");
    assertThat(configuredS3SdkHttpRequest.signingRegionModification()).isEmpty();
    assertThat(configuredS3SdkHttpRequest.sdkHttpRequest().getUri()).isEqualTo(expectedEndpoint);
}
Also used : WriteGetObjectResponseRequest(software.amazon.awssdk.services.s3.model.WriteGetObjectResponseRequest) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) ConfiguredS3SdkHttpRequest(software.amazon.awssdk.services.s3.internal.ConfiguredS3SdkHttpRequest) ConfiguredS3SdkHttpRequest(software.amazon.awssdk.services.s3.internal.ConfiguredS3SdkHttpRequest) URI(java.net.URI)

Aggregations

WriteGetObjectResponseRequest (software.amazon.awssdk.services.s3.model.WriteGetObjectResponseRequest)8 Test (org.junit.jupiter.api.Test)7 Disabled (org.junit.jupiter.api.Disabled)4 WriteGetObjectResponseRequest (com.amazonaws.services.s3.model.WriteGetObjectResponseRequest)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXException (org.xml.sax.SAXException)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 Checksum (com.example.s3objectlambda.checksum.Checksum)1 S3RequestError (com.example.s3objectlambda.error.S3RequestError)1 XMLErrorParser (com.example.s3objectlambda.error.XMLErrorParser)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)1 ConfiguredS3SdkHttpRequest (software.amazon.awssdk.services.s3.internal.ConfiguredS3SdkHttpRequest)1