saving an image to bytes and uploading to boto3 returning content-MD5 mismatch saving an image to bytes and uploading to boto3 returning content-MD5 mismatch python-3.x python-3.x

saving an image to bytes and uploading to boto3 returning content-MD5 mismatch


I had this same problem, and the solution was to seek to the beginning of the saved in-memory file:

out_img = BytesIO()image.save(out_img, img_type)out_img.seek(0)  # Without this line it failsself.bucket.put_object(Bucket=self.bucket_name,                       Key=key,                       Body=out_img)


The file may need to be saved and reloaded before you send it off to S3. The file pointer seek also needs to be at 0.

My problem was sending a file after reading out the first few bytes of it. Opening a file cleanly did the trick.


I found this question getting the same error trying to upload files -- two scripts clashed, one creating, the other uploading. My answer was to create using ".filename" then:

os.rename(filename.replace(".filename","filename"))

The upload script then needs to ignore . files. This ensured the file was done being created.