Search code examples
javaspringamazon-s3spring-integrationspring-integration-aws

How to check if aws s3 bucket available before uploading file using outbound gateway


using aws s3 outbound adapter in spring boot application,trying to upload files in s3 bucket. would like to check if bucket available before uploading file. if bucket is not available need to throw error.

suggest on this.

<int-aws:s3-outbound-gateway id="FileChannelS3"
        request-channel="filesOutS3CChainChannel"
        reply-channel="filesArcChannel"
        transfer-manager="transferManager"
        bucket-expression="headers.TARGET_BUCKET"
                command="UPLOAD">
        <int-aws:request-handler-advice-chain>
            <ref bean="retryAdvice" />          
        </int-aws:request-handler-advice-chain>
    </int-aws:s3-outbound-gateway>

Solution

  • You can configure an S3RemoteFileTemplate bean and use its exists() API in the <filter>:

    <bean id="s3RemoteFileTemplate" class="org.springframework.integration.aws.support.S3RemoteFileTemplate">
        <constructor-arg ref="s3SessionFactory"/>
    </bean>
    
    <int:filter expression="@s3RemoteFileTemplate.exists(headers.TARGET_BUCKET)" throw-exception-on-rejection="true"/>
    

    UPDATE

    Facing below exception java.lang.IllegalStateException: 'path' must in pattern [BUCKET/KEY].

    Sorry, missed the fact that you need to check existence of the bucket, not an object inside.

    For that purpose you need to use an Amazon API directly:

    <int:filter expression="@amazonS3.doesBucketExistV2(headers.TARGET_BUCKET)" throw-exception-on-rejection="true"/>
    

    where an amazonS3 is a bean for the com.amazonaws.services.s3.AmazonS3 client.