Search code examples
androidamazon-web-servicesamazon-s3retrofit2okhttp

Uploading Image to Amazon s3 Retrofit Failure: Timeout


I'm trying to upload a file using a custom url in retrofit.

Here's how I'm making the requestBody:

RequestBody fileBody = RequestBody.create(MediaType.parse(artifactHeaders.getContentType()), file);

Here's the interface:

@PUT
Call<ResponseBody> uploadArtifactToAmazon2(@Url String artifactUrl,
                                           @Header("Content-Disposition") String contentDisposition,
                                           @Header("x-amz-server-side-encryption") String xAmzServerSideEncryption,
                                           @Header("x-amz-security-token") String xAmzSecurityToken,
                                           @Header("Content-Type") String contentType,
                                           @Body RequestBody requestBody);

But I'm getting this on my log:

06-21 10:41:16.354 4760-4808/ 
D/OkHttp: 4�IDATx�<�g�dYv��������j7��cvf�c�5XR����!�06-21 10:41:16.354 4760-4808/D/OkHttp: E�R($�B����"()��D��.�avfz�{zf�Ww�̪J��|/��"������|ޖ�9�?9���Ȧʰ��uv���V���
06-21 10:41:16.504 4760-4808/D/OkHttp: �R>a1�~�`.�>����IOhQS�a�B>��=h0
     [- - - HUNDREDS OF THESE LINES CONTINUE ]
06-21 10:41:16.567 4760-4808/D/OkHttp: Content-Type: image/png
06-21 10:41:16.567 4760-4808/D/OkHttp: Content-Length: 669719

After a couple of seconds the log gives me this:

06-21 10:41:16.626 4760-4808/D/!!!: Cookie Name: __cfduid Cookie Value: df052a1723cd06647966e629e113d4a461466515894 Expiry: Wed, 21-Jun-2017 14:41:14 GMT+00:00
06-21 10:41:28.040 4760-4808/D/!!!: Cookie Name: __cfduid Cookie Value: df052a1723cd06647966e629e113d4a461466515894 Expiry: Wed, 21-Jun-2017 14:41:26 GMT+00:00
06-21 10:41:39.014 4760-4760/D/!!!: Failure Code: timeout

Am I getting the image back? What's wrong?


Solution

  • Finally solved this. It turns out that Retrofit has a default 10 sec. time out (see details here).

    This interrupts or halts the uploading on retrofit's part.

    The characters that I'm seeing on my log (I'm using HttpLoggingInterceptor.Level.BODY). The solution is to set the timeout duration:

    httpClient.readTimeout(10, TimeUnit.MINUTES)
                .connectTimeout(10, TimeUnit.MINUTES)
    

    My next task would be to either determine how long will it take for the file to be uploaded and using that to set the timeout duration OR handle the timeout event correctly.