1

I need to create a process that downloads a file from an API endpoint and uploads it to another API endpoint. The file has a maximum size of 100MB but we will have many processes running in parallel. I'm trying to achieve it with Spring WebClient and without storing the files in memory. The current code is storing the file in memory because the tests with big files are throwing OutOfMemoryError.

Copy method

public Mono<SyncStatusType> copy(Strin path1, String path2) {
    
    return this.download(path1)
        .collectList()
        .flatMap(dataBuffers -> 
            this.upload(path2, Mono.just(dataBuffers).flatMapMany(Flux::fromIterable)))
        .map(item -> SyncStatusType.SUCCESSFUL);
}

Download method

private Flux<DataBuffer> download(String path) {

    return this.getWebClient()
        .get()
        .uri(uriBuilder -> uriBuilder
            .path(path)
            .build())
        .retrieve()
        .bodyToFlux(DataBuffer.class);

}

Upload method

private Mono<CustomFileDto> upload(String path, Flux<DataBuffer> dataBuffer) {
        
    return this.getWebClient()
        .put()
        .uri(uriBuilder -> uriBuilder
            .path(path)
            .build())
        .contentType(MediaType.APPLICATION_JSON)
        .body(BodyInserters.fromDataBuffers(dataBuffer))
        .retrieve()
        .bodyToMono(CustomFileDto.class)
    }

How can I modify this code so that it doesn't store the file content in memory?

1 Answers1

0

I have a Spring WebFlux multipart example with uploading/downloading files and storing them in the Mongo GridFS, check the complete codes on my Github.

The testing codes demonstrated interaction from a WebFlux client.

Hantsy
  • 8,006
  • 7
  • 64
  • 109