AWS S3 Rest API with Android Retrofit V2 library, uploaded image is damaged

后端 未结 6 1564
生来不讨喜
生来不讨喜 2020-12-03 08:57

I\'m trying upload a Image from my Android APP to Amazon AWS S3 and I need use AWS Restful API.

I\'m using Retrofit 2

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 09:34

    You are sending a multipart payload, but forcing the Content-type to be image/jpeg. Your jpg is corrupt because S3 probably saved the multipart headers into your jpg file since you told it the whole message was a JPG. Since you do not actually have multiple parts to send, you can drop the Multipart annotation and use Body instead of Part for your RequestBody

    public interface AwsS3 {
    
        @PUT("/{Key}")
        Call upload(@Path("Key") String Key,
                    @Header("Content-Length") long length,
                    @Header("Accept") String accept,
                    @Header("Host") String host,
                    @Header("Date") String date,
                    @Header("Content-type") String contentType,
                    @Header("Authorization") String authorization,
                    @Body RequestBody body);
    }
    

    You should also be able to remove explicitly setting the Content-type and Content-length headers.

提交回复
热议问题