Search code examples
apache-camelapache-camel-aws

Apache Camel: How to stream large files from AWS?


I have a route as follows. It works for small files, but for large ones (around 6GB or more), my program runs out of memory. How do I stream the content without storing in memory?

public void configure() throws Exception {
    S3LastModifiedFilter lastModifiedFilter =
            new S3LastModifiedFilter(s3Properties.getLastModifiedWithinSeconds(), s3Properties.getPrefix());

    from(inboundS3Uri)
            .filter().method(lastModifiedFilter, "accept")
            .idempotentConsumer(header(S3Constants.KEY),
                    MemoryIdempotentRepository.memoryIdempotentRepository(inboundCacheSize))
            .convertBodyTo(byte[].class, UTF_8.name())
            .process(new ForHttpMessageProcessor(httpProperties))
            .to(outboundHttpUri);
}

Error:

Caused by: org.apache.camel.TypeConversionException: Error during type conversion from type: java.lang.String to the required type: byte[] with value [Body is instance of java.io.InputStream] due java.lang.OutOfMemoryError: Java heap space
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.createTypeConversionException(BaseTypeConverterRegistry.java:629)
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:190)
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:108)
    ... 23 common frames omitted
Caused by: org.apache.camel.RuntimeCamelException: java.lang.OutOfMemoryError: Java heap space
    at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1774)

Solution

  • Turns out the line convertBodyTo(byte[].class, UTF_8.name()) was the problem. It was trying to buffer the content in memory and convert to string. I commented it out, and now the code works.