2

When uploading files to s3 using retrofit uploads successfully and returns a 200 however the file is corrupted. The file can be either a video or image.

 val requestFile = RequestBody.create(MediaType.parse(contentType), file)
 val body = MultipartBody.Part.createFormData(mediaType, task.file_name, requestFile)

assetService.uploadAsset(contentType, task.upload_url, body)

where contentType is either "video/mp4" or "image/jpeg" and mediaType is either "video" or "image"

the service :

@Multipart
@PUT
fun uploadAsset(
    @Header(CONTENT_TYPE) contentType: String,
    @Url uploadUrl: String,
    @Part file: MultipartBody.Part
): Single<ResponseBody>

The files upload and look correct however they are corrupt and cannot be viewed.

I've checked this question but still stuck. AWS S3 Rest API with Android Retrofit V2 library, uploaded image is damaged

nt95
  • 455
  • 1
  • 6
  • 21
  • Why don't you use s3 SDK for uploading? The documentation is pretty clear about how to do it and much better than rest. – p.mathew13 Mar 21 '19 at 14:22
  • we're using a presigned url for the upload similar to this question and so retrofit seemed fitting: https://stackoverflow.com/questions/45991720/upload-a-file-to-aws-s3-pre-signed-url-using-retrofit2 – nt95 Mar 21 '19 at 14:25
  • Are you saying that each time u are uploading to different buckets according to your api response? – p.mathew13 Mar 21 '19 at 14:28
  • different upload urls for each asset. The URLs are correct and i get a 200 response from the call however when i go to view the videos/images on S3 they are corrupt. The error must be within the requestbody – nt95 Mar 21 '19 at 14:32

2 Answers2

3

Removed Multipart upload and it worked.

nt95
  • 455
  • 1
  • 6
  • 21
  • Why we need to remove multipart from retrofit API call?.S3 will do it automatically? – Nithinjith Apr 12 '19 at 05:38
  • The urls our api was generating for S3 could not handle multipart upload and so by removing this and uploading as a Body it worked perfectly – nt95 Apr 12 '19 at 15:06
  • Hi I am facing the exact same issue? Did your removed multipart from ApiInterface? – Dhruv Kaushal Jan 22 '20 at 15:18
  • @DhruvKaushal yes I removed multipart and all worked fine. Our upload urls did not support multipart uploading to AWS. Hope it works for you! – nt95 Jan 23 '20 at 11:21
0

as an improvement and elaboration on @nt95's answer, you are using the multipart to send the file to the server, while it seems unnecessary to do so, just create a RequestBody out of the desired file and send it as a @Body in parameters.

not working code:

@Multipart
@PUT
fun uploadAsset(
@Header(CONTENT_TYPE) contentType: String,
@Url uploadUrl: String,
@Part file: MultipartBody.Part
): Single<ResponseBody>

working code:

val requestFile = RequestBody.create(MediaType.parse(contentType), file)

and pass it to the interface as follows:

@PUT
fun uploadAsset(
@Header(CONTENT_TYPE) contentType: String,
@Url uploadUrl: String,
@Body file: RequestBody
): Single<ResponseBody>

and you will be fine