0

I have 2 microservices. The first service(video converter) and the second service(integration Spring with Amazon S3 to store video and image on the Amazon S3).

The first service produces files by schedule and after converts File to MultipartFile uploads it to the second service and after to Amazon S3.

But I stack on the issue with Feigh uploading the video. When I debug, I saw that the file in @RequestPart in the second service is null. But converting was done successfully.

I try adding encoding as in that post File upload spring cloud feign client, but that does not help.

Code sample first microservice: Upload methods:

    @Override
    public Response<String> uploadFile(final Object object, final MultipartFile multipartFile) {
        final Map<String, Object> s3Url = videoServiceFeign.uploadFile(multipartFile,
                object.getId().toString(), object.getIdIds().toString(), object.getName());
        object.setS3Url(s3Url.get("object").toString());
        return generateSuccessResponse();
    }

    @Override
    public Response<String> uploadFile(final Stream stream, final File file) throws IOException {
        final InputStream inputStream = Files.newInputStream(file.toPath());
        final MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                ContentType.MULTIPART_FORM_DATA.toString(), IOUtils.toByteArray(inputStream));
        return this.uploadFile(stream, multipartFile);
    }

Feign client:

@FeignClient(name = "video-service", url = "${video-service.ribbon.listOfServers}")
public interface VideoServiceFeign {

    @PostMapping(path = "/api/v1/video/upload", consumes = {"multipart/form-data"})
    Map<String, Object> uploadFile(@RequestPart("file") MultipartFile multipartFile,
                                   @RequestParam("id") String id,
                                   @RequestParam("ids") String ids,
                                   @RequestParam("name") String name);

}

Upload endpoint in the second service:

    @ApiOperation("Upload new video file")
    @ApiResponses({
            @ApiResponse(code = 200, message = "File uploaded successfully"),
            @ApiResponse(code = 403, message = "Access Denied"),
            @ApiResponse(code = 500, message = "Internal server error"),
            @ApiResponse(code = 503, message = "Gateway timeout")
    })
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/upload", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
    public @ResponseBody
    Map<String, Object> uploadFile(@RequestPart("file") final MultipartFile file,
                                   @RequestParam("id") final String id,
                                   @RequestParam("ids") final String ids,
                                   @RequestParam("name") final String name
                                ) throws IOException {
        return uploadService.uploadFile(id, ids, name, file);
    }

What am I missing?

Tomash Gombosh
  • 143
  • 1
  • 3
  • 18

1 Answers1

0

After a few days of researching, I found what I miss, the issue was with that method: public Response<String> uploadFile(final Stream stream, final File file) throws IOException

In the row:

final MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.MULTIPART_FORM_DATA.toString(), IOUtils.toByteArray(inputStream));

I had two issues first is the content type, I use video/mp4 not ContentType.MULTIPART_FORM_DATA. And the second one is the first param in the constructor of MockMultipartFile I use the file.getName(), but that is incorrect because the docs say that is a field name in the multipart request. And that should be "file".

Tomash Gombosh
  • 143
  • 1
  • 3
  • 18