Adding metadata data to S3 object using S3 outbound gateway

621 Views Asked by At

have written code for spring integration aws application, which currently upload files from source folder to target S3 bucket using s3-outbound-gateway. Would like to add below user defined metadata and their value about the file while uploading the file in S3 bucket.

x-amz-meta-sourcePath : /test

x-amz-meta-targetPath :/targetbucket/

x-amz-meta-timestamp : (#timestamp value)

Would like to know how to add metadata using S3 outbound gateway or other way. Any documentation/example/suggestion will be helpful to achieve this.

<util:map id="userMetadata" value-type="java.lang.String">
            <entry key="x-amz-meta-source" value="testsource" />
            <entry key="x-amz-meta-sourcePath" value="testpath" />    
            <entry key="x-amz-meta-targetPath" value="headers.TARGET_PATH" />               </util:map>

        <bean id="objectMetadata"  class="com.amazonaws.services.s3.model.ObjectMetadata">
        <property name="userMetadata" ref="userMetadata"/>
        </bean>

        <bean id="uploadMetadataProvider"  class="org.springframework.integration.aws.outbound.S3MessageHandler$UploadMetadataProvider">
        <property name="objectMetadata" ref="objectMetadata"/>
        </bean>
<int-aws:s3-outbound-gateway id="s3File"
        request-channel="filesS3GateWay"
        reply-channel="filesS3ChainChannel"
        transfer-manager="transferManager"
        bucket-expression = "headers.TARGET_PATH"
        key-expression="headers.file_name"
        upload-metadata-provider="uploadMetadataProvider"
        command="UPLOAD">
        <int-aws:request-handler-advice-chain>
            <ref bean="retryAdvice" />
        </int-aws:request-handler-advice-chain>
    </int-aws:s3-outbound-gateway>
1

There are 1 best solutions below

4
On BEST ANSWER

I think what you need is this:

    <xsd:attribute name="upload-metadata-provider">
        <xsd:annotation>
            <xsd:documentation>
                Reference to an instance of
                'org.springframework.integration.aws.outbound.S3MessageHandler$UploadMetadataProvider'.
            </xsd:documentation>
            <xsd:appinfo>
                <tool:annotation kind="ref">
                    <tool:expected-type
                            type="org.springframework.integration.aws.outbound.S3MessageHandler$UploadMetadataProvider"/>
                </tool:annotation>
            </xsd:appinfo>
        </xsd:annotation>
    </xsd:attribute>

So, you inject such a UploadMetadataProvider:

/**
 * The callback to populate an {@link ObjectMetadata} for upload operation.
 * The message can be used as a metadata source.
 */
public interface UploadMetadataProvider {

    void populateMetadata(ObjectMetadata metadata, Message<?> message);

}

And populate into that ObjectMetadata whatever you need based on the request message if that.