[英]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 {公共 class SQSListener 实现 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.我在 s3 中添加了一个文件,然后将消息发送到 sqs 队列。 after getting this message can I get the actual data, that was uploaded in s3?收到此消息后,我可以获得在 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.从您的问题中不清楚消息包含什么,但是如果消息包含 S3 存储桶和密钥,那么是的,您可以使用 S3 API 来下载它。
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.此逻辑将创建 S3 客户端并下载 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.这将为您提供一个可以读取的 InputStream。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.