简体   繁体   English

带有retrofit2视频/图像损坏的AWS S3文件上传

[英]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.当使用改造上传成功上传文件到 s3 并返回 200 但文件已损坏。 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"其中 contentType 是“video/mp4”或“image/jpeg”,mediaType 是“video”或“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 带有 Android Retrofit V2 库的 AWS S3 Rest API,上传的图像已损坏

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

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.作为对@nt95 答案的改进和详细说明,您正在使用 multipart 将文件发送到服务器,虽然似乎没有必要这样做,但只需从所需文件中创建一个RequestBody并将其作为参数中的@Body发送。

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你会没事的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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