Set S3 bucket dynamically using S3 Message Handler and Spring Integration

Viewed 550

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;
1 Answers
Related