How to upload large files to S3 using presigned URL?

2.4k Views Asked by At

I have been able to upload successully small files ~1kb but when I try uploading larger files > 1Mb I get this exception:

java.io.IOException: Error writing to server
    at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:699)
    at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:711)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1585)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347)
    at com.red5.VimeoTechRojoIntegration.uploadFromEFSToS3(VimeoTechRojoIntegration.java:209)
    at com.red5.Red5ProLive$1.run(Red5ProLive.java:128)
    at java.lang.Thread.run(Thread.java:748)

To upload I am suing this code

OutputStream out = null;
InputStream in = null;
HttpURLConnection connection = null;
try {
    URL url = new URL(signedURLS3);
    in = new FileInputStream(videoEFSPath);

    System.out.println("establish connection");
    connection=(HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("PUT");
    connection.setRequestProperty("Content-Type", "application/octet-stream"); 


    System.out.println("get output stream");
    out = (OutputStream) connection.getOutputStream();
    System.out.println("copy");
    IOUtils.copyLarge(in,out);
    System.out.println("copy finished");

    int responseCode = connection.getResponseCode();
    if (responseCode < 300)
        return "";

    return "{\"error\":\"The upload to S3 failed. AWS server returned response code "+responseCode+"\"}";   
}

What is the problem?

1

There are 1 best solutions below

0
On

This should actually work, I am using Apache HTTP Client for this:

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.FileEntity;

...

String presignedUrl = ...
File file = ...

HttpPut httpPut = new HttpPut(presignedUrl);
httpPut.setEntity(new FileEntity(file));
HttpResponse httpResponsePut = httpClient.execute(httpPut);
if (httpResponsePut.getStatusLine().getStatusCode() >= HttpStatus.SC_BAD_REQUEST) {
    log.error("Error uploading file: " + file.getName() + " / " + httpResponsePut);
}
EntityUtils.consume(httpResponsePut.getEntity());