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

孤街浪徒 提交于 2019-12-24 11:36:31

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/51867049/how-to-check-if-aws-s3-bucket-available-before-uploading-file-using-outbound-gat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!