I am using this code in Kotlin/Android to upload image to server:
fun uploadImageWithLambda(){
val file = File(filePath)
val retrofit = NetworkClient.getRetrofitObj()
val uploadApis = retrofit.create(UploadApis::class.java)
val requestBody = RequestBody.create(MediaType.parse("image/*"), file)
val part = MultipartBody.Part.createFormData("image", file.name, requestBody)
val call = uploadApis.uploadImage(imageRequest = part)
call.enqueue(object : Callback<ImageResponse> {
override fun onFailure(call: Call<ImageResponse>, t: Throwable) {
Log.e(TAG, t.toString())
}
override fun onResponse(call: Call<ImageResponse>, response: Response<ImageResponse>) {
Log.i(TAG, "success")
}
})
}
This is function in UploadApis
@Multipart
@POST("prod/res")
fun uploadImage(@Part imageRequest: MultipartBody.Part) : Call<ImageResponse>
The file that I received in Lambda cannot be open. When I removed some bytes from that file I got proper image file.
First lines of code in my lambda function:
const multipart = require('aws-multipart-parser')
exports.handler = async (event) => {
try {
CONSTANTS.logger.info("POST image in S3...")
let spotText = {}
const result = multipart.parse(event, spotText)
fs.writeFileSync('original.jpg', result.image.content)
const content = result.image.content;
console.log(result)
var newBuffer = new Buffer(content.length - 22);
content.copy(newBuffer, 0, 22, content.length);
// const sliced = content.slice(25);
console.log(newBuffer);
fs.writeFileSync('new.jpg', content);
....
I also made a small express application with one route to upload the image to S3 and I sent the image from Android in the same way and it worked, I could open the image. Is there anyone having the same/similar issue?