#StackBounty: #android #amazon-s3 #retrofit #retrofit2 #aws-sdk Retrofit2-AWS S3 Multipart image File corrupt issue

Bounty: 50

I am using Retrofit2 for handling API calls in my Android application. The application contains several image upload features. All the image uploading features are wired up to two API calls and will fire one after another.

  1. The first API will upload the image content to the server and the server will generate a dynamic signed AWS S3 image URL as a response.

  2. The second API is using the above-signed URL and PUT the data into AWS S3.

It was working perfectly for small images. For large images, the API has failed. The possible solution for this issue is to change the upload API as multipart. So I have changed the second API as “Retrofit2-multipart“. But the problem here is, After uploading the image, the file got corrupted in the S3.

So my question is, Is retrofit multipart is fit for ASW S3 upload?
Do we have any solution to solve this issue?.

Please check my Implementation

Retrofit API Interface

 @Multipart
    @PUT
    Call<Void> uploadFile(@Url String url,
                          @Part MultipartBody.Part file);

Retrofit service creator

 private ApiServicesList createService() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(300, TimeUnit.SECONDS)
                .connectTimeout(360, TimeUnit.SECONDS)
                .cache(null)
                .addInterceptor(new NetworkInterceptor(context))
                .addInterceptor(createLoggingInterceptor())
                .addInterceptor(createSessionExpiryInterceptor())
                .addInterceptor(createContextHeaderInterceptor())
                .build();

        return new Retrofit.Builder()
                .baseUrl(FirebaseConfig.getInstance().getStagingBaseURl())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build()
                .create(ApiServicesList.class);
    }

File Upload Class

 public void uploadFileTos3(String url, MultipartBody.Part file, Callback<Void> listener) {
        Call<Void> call = mAPIServices.uploadFile(url,file);
        call.enqueue(listener);
    }

Presenter API Call

networkServices.uploadFileTos3(credentials.getSignedUrl(), prepareRequestBody(file), new Callback<Void>() {
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
               //App Action
            }

            @Override
            public void
            onResponse(Call<Void> call, Response<Void> response) {
               //App Action
            }
        });

Request Body Method

 public static MultipartBody.Part prepareRequestBody(File file) {
        RequestBody requestFile = RequestBody.create(MediaType.parse(CONTENT_TYPE), file); // Checked Both "image/jpeg" and "multipart/form-data"
        return  MultipartBody.Part.createFormData("image", file.getName(), requestFile);
    }

DDMS LOGS

2019-04-10 19:07:13.926 18761-18874/com.xxx D/OkHttp: --> PUT https:Signed URL(Removed actual URL)
2019-04-10 19:07:13.926 18761-18874/com.xxx D/OkHttp: Content-Type: multipart/form-data; boundary=60561d1c-ff3f-4a43-8022-ca2be3e8ec4e
2019-04-10 19:07:13.926 18761-18874/com.xxx D/OkHttp: Content-Length: 71154
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: --60561d1c-ff3f-4a43-8022-ca2be3e8ec4e
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Content-Disposition: form-data; name="image"; filename="forest-trees-fog-foggy.jpg"
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Content-Type: image/jpeg
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Content-Length: 70934
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Image Body
2019-04-10 19:07:13.942 18761-18874/com.xxx D/OkHttp: --60561d1c-ff3f-4a43-8022-ca2be3e8ec4e--
2019-04-10 19:07:13.942 18761-18874/com.xxx D/OkHttp: --> END PUT (71154-byte body)
2019-04-10 19:07:15.648 18761-18874/com.xxx D/OkHttp: <-- 200 OK


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.