简体   繁体   English

如何从 s3 自动获取 java 中的文件?

[英]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.

相关问题 我们如何从S3亚马逊服务器打开文件URL中获取Java中的File()类型对象? - How can we get File() type object in Java from S3 amazon server open file URL? 如何从 java 中的 s3 获取触发 lambda 的文件名 - how to get name of file that trigger lambda from s3 in java 如何使用 Kotlin/Java 从我的 s3 存储桶中读取 Excel 文件 (xlsx)? - How can I read an Excel file (xlsx) from my s3 bucket with Kotlin/Java? 如何将数据从Amazon S3复制到自动红移? - How can I copy data from amazon S3 to redshift automatically? 带有Java的AWS Lambda无法从S3获取文件 - AWS Lambda with Java unable to GET a file from S3 如何将 AWS S3 存储桶中的 excel 文件放入 Java 中的 MultipartFile - How to get an excel file from AWS S3 bucket into a MultipartFile in Java 如何使用ListVersionsRequest,JAVA从S3获取确切文件的信息? - How to get the information of the exact file from S3 using ListVersionsRequest, JAVA? 当密钥使用文件夹结构定义时,我们如何从 AWS s3 存储桶中获取 Object? Java - How can we get the Object from the AWS s3 bucket when the key is defined with folder structure? Java 如何从 jar 而不是 .java 文件在 S3 上创建私有 Maven 存储库? - How do I create a private Maven repository on S3 from jar not .java file? 如何从java中的s3输入流中获取值 - How to get the value from the s3 input stream in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM