1

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>
Jessie
  • 963
  • 5
  • 16
  • 26

1 Answers1

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.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Just need some clarification. Am running this application in ec2 instance where it as direct connection with S3 bucket. No credentials am using in my application to upload files in S3 bucket. Directly using outbound adapter to upload the file.. will this S3RemoteFileTemplate work. If I dont give any session factory details? Could you suggest how to check for this scenario. – Jessie Aug 16 '18 at 20:52
  • See `S3SessionFactory`: it is based on the `AmazonS3Client`. You can use there exactly what `TransferManager` do: `new AmazonS3Client(new DefaultAWSCredentialsProviderChain()` – Artem Bilan Aug 16 '18 at 21:28
  • Implemented the solution. Facing below exception java.lang.IllegalStateException: 'path' must in pattern [BUCKET/KEY]. Could you suggest on this. – Jessie Aug 17 '18 at 13:32
  • Seen an UPDATE in my answer, please. – Artem Bilan Aug 17 '18 at 14:29
  • Its working. need some suggestion. is it possible to do any retry mechanism for three times if bucket not available...like we do in spring retry. – Jessie Aug 17 '18 at 15:24
  • Yes, you can use that. As long as you have a `throw-exception-on-rejection="true"` the retry advice will do the job! – Artem Bilan Aug 17 '18 at 15:28