我有一个端点,它的目的是接收一个csv文件,对它的名称做一些更改,然后将它发送到一个方法,将它包含在一个文件中的所有数据以纯文本形式上传到Google。
该文件可能有超过10万条记录,所以在解析它时,我必须将所有数据保存在一个变量中,然后将其保存在Google中。今天我可以这样做,但是我总是覆盖同一个文件,因为我不知道如何在方法中指示等待订阅的完整进程,然后再上传文件,所以每次数据被添加到数组时,文件都会再次上传。
虽然该方法符合这一思想,但我想提高这个性能,因为上传一个只有2MB的文件和10万条记录大约需要15分钟。有什么想法吗?
private Storage storage;
private void uploadToGoogleCloudStorage(FilePart filePart, BlobInfo blobInfo) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
filePart.content()
.subscribe(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer);
try {
bos.write(bytes);
storage.createFrom(blobInfo, new ByteArrayInputStream(bos.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}发布于 2022-03-16 13:06:50
我终于找到解决办法了。我将订阅更改为map,然后从通量中获得最后一个响应,然后订阅响应,使用存储接口将数据上传到google云存储(使用他们的api的包来自google )
private Storage storage;
private void uploadToGoogleCloudStorage(FilePart filePart, BlobInfo blobInfo) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
filePart.content()
.map(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer);
try {
bos.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
return bos;
}).last().subscribe(data -> {
try {
storage.createFrom(blobInfo, new ByteArrayInputStream(bos.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}https://stackoverflow.com/questions/71443676
复制相似问题