且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在使用出站网关上传文件之前检查AWS S3存储桶是否可用

更新时间:2023-11-27 09:19:46

您可以配置S3RemoteFileTemplate bean,并在<filter>中使用其exists() API:

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"/>

更新

面对异常java.lang.IllegalStateException: 'path' must in pattern [BUCKET/KEY].

对不起,您错过了需要检查bucket而不是内部对象的事实.

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

为此,您需要直接使用Amazon API:

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

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

其中amazonS3com.amazonaws.services.s3.AmazonS3客户端的bean.

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