我试图弄清楚为什么通过 WebFlux 端点将文件上传到 minio(符合 S3 的文档存储)没有完成;我总是只将 4kb 的文件放入 Minio。
我的端点:
public Mono<ServerResponse> uploadFile(ServerRequest request) {
log.info("Uploading file...");
log.info("Content Type: {}", request.headers().contentType().orElse(MediaType.TEXT_PLAIN));
return request.body(BodyExtractors.toMultipartData())
.flatMap(map -> {
Map<String, Part> parts = map.toSingleValueMap();
return Mono.just((FilePart) parts.get("files"));
})
.flatMap(this::saveFile)
.flatMap(part -> ServerResponse.ok().body(BodyInserters.fromObject(part)));
}
private Mono<String> saveFile(FilePart part) {
return part.content().map(dataBuffer -> {
try {
log.info("Putting file {} to minio...", part.filename());
client.putObject("files", part.filename(), dataBuffer.asInputStream(), part.headers().getContentType().getType());
} catch(Exception e) {
log.error("Error storing file to minio", e);
return part.filename();
}
return part.filename();
}).next();
}
我很确定这是一个阻塞与非阻塞的问题,但是如果我尝试在其中添加一个blockFirst()
调用,我会收到一个异常,说它在运行时是不允许的。
有没有办法有效地流式传输数据,或者这是 Minio 客户端与 WebFlux 不兼容的情况?
我正在尝试从这样的 React 组件发布:
class DataUpload extends React.Component {
constructor(props) {
super(props);
this.state = {
fileURL: '',
};
this.handleUploadData = this.handleUploadData.bind(this);
}
handleUploadData = ev => {
ev.preventDefault()
const data = new FormData();
data.append('files', this.uploadInput.files[0]);
data.append('filename', this.fileName.value);
fetch('http://localhost:8080/requestor/upload', {
method: 'POST',
body: data,
}).then((response) => {
response.json().then((body) => {
this.setState({ fileURL: `http://localhost:8080/${body.file}`});
});
});
}
render() {
return (
<form onSubmit={this.handleUploadData}>
<div>
<input ref={(ref) => { this.uploadInput = ref; }} type="file" />
</div>
<div>
<input ref={(ref) => { this.fileName = ref; }} type="text" placeholder="Enter the desired name of the file" />
</div>
<br/>
<div><button>Upload</button></div>
</form>
);
}
}
export default DataUpload;