简体   繁体   中英

AWS S3 file upload with retrofit2 video/image corrupt

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

删除了分段上传,并且可以正常工作。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM