I want to upload a file to an object storage minio which created a presigned URL using a Java client API.
In the documentation only refers to creating presigned URL or creating some. Is there a to upload using the presigned url.
I want to upload a file to an object storage minio which created a presigned URL using a Java client API.
In the documentation only refers to creating presigned URL or creating some. Is there a to upload using the presigned url.
I think you probably got the answer by now, gonna put it here for others who might have stumbled upon similar task.
There are a few different HTTP clients that you could use in JAVA, so implementations may vary. The idea is, once you get the URL, it is just a matter of sending an HTTP PUT
request using the URL with the file's binary content, just like you would do in any file upload procedure. As far as I know, you cannot send multipart file data directly using PUT
, you have to send binary stream.
Here's an example of uploading a jpeg file with OkHttpClient
:
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("image/jpeg");
RequestBody body = RequestBody.create(mediaType, "<file contents here>");
Request request = new Request.Builder()
.url("<minio presigned url here>")
.method("PUT", body)
.addHeader("Content-Type", "image/jpeg")
.build();
Response response = client.newCall(request).execute();
Another example with Spring's RestTemplate
where the incoming request to the controller is a MultipartFile
. If it's a File
object instead, you can use your favorite utility method such as byte[] org.apache.commons.io.FileUtils.readFileToByteArray(File file)
to get a byte array from that file.
HttpHeaders headers = new HttpHeaders();
HttpEntity<byte[]> entity = new HttpEntity<>(multipartFile.getBytes(), headers);
restTemplate.exchange(new URI("<presignedUrl>"),
org.springframework.http.HttpMethod.PUT, entity, Void.class);
You can search for your specific HTTP Client, just need to look for "RESTful file upload with PUT request" Or something similar.