简体   繁体   中英

How can I automatically get file in java from s3?

@Value("${amazon.sqs.queue.endpoint}")
private String endpoint;
@Value("${amazon.sqs.queue.name}")
private String queueName;
@Autowired
private SQSListener sqsListener;

@Bean
public DefaultMessageListenerContainer jmsListenerContainer() throws JMSException {
    SQSConnectionFactory sqsConnectionFactory = SQSConnectionFactory.builder()
            .withAWSCredentialsProvider(new DefaultAWSCredentialsProviderChain())
            .withEndpoint(endpoint)
            .withAWSCredentialsProvider(new ClasspathPropertiesFileCredentialsProvider("AwsCredentials-sqs.properties"))
            .withNumberOfMessagesToPrefetch(10).build();

    DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
    dmlc.setConnectionFactory(sqsConnectionFactory);
    dmlc.setDestinationName(queueName);
    dmlc.setConcurrentConsumers(1);
    dmlc.setMaxConcurrentConsumers(100);

    dmlc.setMessageListener(sqsListener);

    return dmlc;
}




@Component

public class SQSListener implements MessageListener {

private static final Logger LOGGER = LoggerFactory.getLogger(SQSListener.class);

@Override
public void onMessage(Message message) {
    try {
        // Cast the recei

ved message as TextMessage and print the text to screen.
            System.out.println("Received: " + ((TextMessage) message).getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}

I added a file in s3 then the message was sent to sqs queue. after getting this message can I get the actual data, that was uploaded in s3?

It's not clear from your question what the message contains, but if the message contains the S3 bucket and key, then yes, you can just use the S3 API to download this.

Add the following dependency

<dependency>
  <groupId>software.amazon.awssdk</groupId>
  <artifactId>s3</artifactId>
</dependency>

This logic will create the S3 client and download the object.

AwsCredentials credentials = AwsBasicCredentials.create(
    "<AWS Access Key>",
    "<AWS Secret>"
);

S3Client s3client = S3Client.builder()
    .region(Region.US_EAST_1) // or whatever region you're in
    .credentialsProvider(() -> credentials) // credentials created above (or preferably injected)
    .build();

GetObjectRequest getObjectRequest = GetObjectRequest.builder()
    .bucket("the bucket") // the bucket that contains the object, from message maybe?
    .key("the key") // the key to the object, from message maybe?
    .build();

ResponseInputStream<GetObjectResponse> responseInputStream = s3client.getObject(getObjectRequest);

This will give you an InputStream that you can read from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM