spring 批处理文件编写器直接写入亚马逊 s3 存储,无需 PutObjectRequest [英] spring batch file writer to write directly to amazon s3 storage without PutObjectRequest
问题描述
我正在尝试将文件上传到亚马逊 s3.我不想上传,而是想使用 spring 批处理从数据库中读取数据并将文件直接写入 s3 存储.无论如何我们可以做到吗?
I'm trying to upload a file to amazon s3. Instead of uploading, I want to read the data from database using spring batch and write the file directly into the s3 storage. Is there anyway we can do that ?
推荐答案
Spring Cloud AWS 添加支持 Amazon S3 服务以使用资源加载器和 s3 协议加载和写入资源.一旦你有 配置 AWS 资源加载器,您可以编写自定义 Spring Batch 编写器,例如:
Spring Cloud AWS adds support for the Amazon S3 service to load and write resources with the resource loader and the s3 protocol. Once you have configured the AWS resource loader, you can write a custom Spring Batch writer like:
import java.io.OutputStream;
import java.util.List;
import org.springframework.batch.item.ItemWriter;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.WritableResource;
public class AwsS3ItemWriter implements ItemWriter<String> {
private ResourceLoader resourceLoader;
private WritableResource resource;
public AwsS3ItemWriter(ResourceLoader resourceLoader, String resource) {
this.resourceLoader = resourceLoader;
this.resource = (WritableResource) this.resourceLoader.getResource(resource);
}
@Override
public void write(List<? extends String> items) throws Exception {
try (OutputStream outputStream = resource.getOutputStream()) {
for (String item : items) {
outputStream.write(item.getBytes());
}
}
}
}
然后你应该能够使用这个编写器和一个 S3 资源,比如 s3://myBucket/myFile.log
.
Then you should be able to use this writer with an S3 resource like s3://myBucket/myFile.log
.
无论如何我们能做到吗?
Is there anyway we can do that ?
请注意,我没有编译/测试之前的代码.我只是想给你一个如何去做的起点.
Please note that I did not compile/test the previous code. I just wanted to give you a starting point of how to do it.
希望这会有所帮助.
这篇关于spring 批处理文件编写器直接写入亚马逊 s3 存储,无需 PutObjectRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!