2

How can I pass in an 'asset' POJO to the S3MessageHandler using Spring Integration and change the bucket?

I would like to be able to change the bucket, based on a folder in the asset, to something like 'root-bucket/a/b/c.'

Each asset can potentially have a different sub-path.

    @Bean
    IntegrationFlow flowUploadAssetToS3()
    {
        return flow -> flow
            .channel(this.channelUploadAsset)
            .channel(c -> c.queue(10))
            .publishSubscribeChannel(c -> c
                .subscribe(s -> s
                    // Publish the asset?
                    .<File>handle((p, h) -> this.publishS3Asset(
                        p,
                        (Asset)h.get("asset")))
                    // Set the action
                    .enrichHeaders(e -> e
                        .header("s3Command", Command.UPLOAD.name()))
                    // Upload the file
                    .handle(this.s3MessageHandler())));
    }

    MessageHandler s3MessageHandler()
    {
        return new S3MessageHandler(amazonS3, "root-bucket");
    }

Thanks to the answer below, I got this working like so:

final String bucket = "'my-root";
final Expression bucketExpression = PARSER.parseExpression(bucket);

final Expression keyExpression = PARSER.parseExpression("headers['asset'].bucket");

final S3MessageHandler handler = new S3MessageHandler(amazonS3, bucketExpression);
handler.setKeyExpression(keyExpression);
return handler;
KJQ
  • 447
  • 1
  • 7
  • 28

1 Answers1

0

There is bucketExpression option which is SpEL to let to resolve a bucket from the requestMessage:

 private static SpelExpressionParser PARSER = new SpelExpressionParser();

 MessageHandler s3MessageHandler() {
    Expression bucketExpression =
                PARSER.parseExpression("payload.bucketProperty");
    return new S3MessageHandler(amazonS3, bucketExpression);
}

Also be aware that root-bucket/a/b/c. isn't bucket name. You should consider to distinguish to bucket and the key to build that complex sub-folder path. For that purpose there is keyExpression option with similar functionality to resolve the key in bucket against requestMessage.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Just out of curiosity, can I set these expressions through the DSL above or would it have to be inside the method returning the handler? – KJQ Jul 20 '17 at 19:16
  • There is no Java DSL for AWS Channel Adapter. So, you have to use setters and ctors of the `S3MessageHandler` directly. – Artem Bilan Jul 20 '17 at 19:17