Error uploading file on AWS android retrofit
I get a AWS S3 bucket URL when I call an API On the s3 URL I am trying to upload a file via retrofit But I am getting the following error
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><AWSAccessKeyId>AKIA6F7T6E47MWT7QUOT</AWSAccessKeyId><StringToSign>PUT
multipart/form-data; boundary=754bc465-aad0-41d4-8ac2-2f333ec2c011
1642929217
/plnms-devappmedia/payment-receipt/252ec59e-0947-42e9-9d11-dd8f7eca0902.jpg</StringToSign><SignatureProvided>mcUVkLkJCtrlG5E0X+9uG3Yh5QA=</SignatureProvided><StringToSignBytes>50 55 54 0a 0a 6d 75 6c 74 69 70 61 72 74 2f 66 6f 72 6d 2d 64 61 74 61 3b 20 62 6f 75 6e 64 61 72 79 3d 37 35 34 62 63 34 36 35 2d 61 61 64 30 2d 34 31 64 34 2d 38 61 63 32 2d 32 66 33 33 33 65 63 32 63 30 31 31 0a 31 36 34 32 39 32 39 32 31 37 0a 2f 70 6c 6e 6d 73 2d 64 65 76 61 70 70 6d 65 64 69 61 2f 70 61 79 6d 65 6e 74 2d 72 65 63 65 69 70 74 2f 32 35 32 65 63 35 39 65 2d 30 39 34 37 2d 34 32 65 39 2d 39 64 31 31 2d 64 64 38 66 37 65 63 61 30 39 30 32 2e 6a 70 67</StringToSignBytes><RequestId>356AM1RVYQJRDBHM</RequestId><HostId>cMj3x6+X2Er0lSFHqDSaWCbKOXNw8qlNVqst7RIMllSyUr9bvkKn305dJRTqd31shmTmbLa972A=</HostId></Error>
I am doing a multipart upload via PUT method. On postman it is working but not from Android
val requestFile: RequestBody = file.name.toRequestBody("image/jpeg".toMediaTypeOrNull())
val body: MultipartBody.Part = MultipartBody.Part.createFormData("image", file.name, requestFile)
val res= executeApiCall { octaveUserApi.uploadFile(uploadUrl,body)}
The releveant retrofit
@Multipart
@PUT
suspend fun uploadFile(
@Url url:String,
@Part filepart: MultipartBody.Part
): Response<String>
Solution 1:
It was a binary upload and I was doing it by multipart which was failing
Retrofit call
@PUT
suspend fun uploadFile(
@Url url:String,
@Body filebody: RequestBody
): Response<RequestBody>
Request body would be
Api.uploadFile(uploadUrl,file.asRequestBody("image/jpeg".toMediaTypeOrNull()))