如何在使用出站网关上传文件之前检查AWS S3存储桶是否可用 [英] How to check if aws s3 bucket available before uploading file using outbound gateway
问题描述
在spring boot应用程序中使用aws s3出站适配器,尝试将文件上传到s3存储桶中.想要在上传文件之前检查存储桶是否可用.如果存储桶不可用,则需要引发错误.
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.
对此建议.
<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>
推荐答案
您可以配置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"/>
其中amazonS3
是com.amazonaws.services.s3.AmazonS3
客户端的bean.
where an amazonS3
is a bean for the com.amazonaws.services.s3.AmazonS3
client.
这篇关于如何在使用出站网关上传文件之前检查AWS S3存储桶是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!