kaliber-scala / play-s3

S3 module for Play
MIT License
119 stars 49 forks source link

Can only upload text files. #70

Closed narayanjr closed 8 years ago

narayanjr commented 8 years ago

Using Play 2.4.1 and play-s3 version 7.0.2

I am able to upload text files just fine, but when I try anything more complex, images, .ods, .jar files it fails with this error The provided 'x-amz-content-sha256' header does not match what was computed.

If I swap "avatar.jpeg" for "text.txt" and "image/jpeg" for "plain/text" in the following code it works. The text file gets uploaded to S3. But if I try and upload any thing else it fails saying the header doesn't match. I've tried png, jpg, jpeg, ods, and jar files.

val file_path = "/path/to/file/avatar.jpeg"
val bucket = S3("path_to_bucket")
val byte_array = Files.readAllBytes(Paths.get(file_path))

val result = bucket + BucketFile("avatar.jpeg", "image/jpeg", byte_array)
result.map { unit =>
    Logger.info("Saved the file")
}
    .recover {
        case S3Exception(status, code, message, originalXml) =>
        {
            Logger.info("Error: " + message)
            Logger.info("originalXml: " + originalXml)
        }
    }
EECOLOR commented 8 years ago

@narayanjr Let me see if I can reproduce the problem.

EECOLOR commented 8 years ago

I can not reproduce the problem and I can upload my slack avatar without a problem. Could you post the result from your log file?

narayanjr commented 8 years ago

This is the "original xml" logged message and the example image that generated this message. avatar

<Error>
   <Code>XAmzContentSHA256Mismatch</Code>
   <Message>The provided 'x-amz-content-sha256' header does not match what was computed.</Message>
   <ClientComputedContentSHA256>6d32a470a69d0c3af4b345e8a543ede0a425180bca616eba903ba32f35ee31b2</ClientComputedContentSHA256>
   <S3ComputedContentSHA256>949d177392b7651e8c1fa67239a777f2b9fb2e6f5b9898e83a74dab01f5f010c</S3ComputedContentSHA256>
   <RequestId>16ACAA3A2A2AE950</RequestId>
   <HostId>QJUy+PYrgOuFKw6mIwDBA9WVXIkRJH3cQx/znKhqCT0X9LtwVmc6bmmH3HJqO/38znbGvXtp0TA=</HostId>
</Error>
narayanjr commented 8 years ago

Hmmm, I just created another play project just to test this single thing and it seems to work as expected there. So there is something wrong with my other app. However I have no idea how/why it's sha256 hash could be different.

EECOLOR commented 8 years ago

Ohw, that is nasty. Those types of bugs are very hard to trace.

A few things to check:

I can not think of anything else. If none of the above works, remove halves of the code until you find the offending part (binary search). Please report back once you found the problem so others don't need to go through the same struggle.

narayanjr commented 8 years ago

So I ended up resorting to cloning the broken project and slowly working my way to making both the working example and broken project look identical. After doing this for a few hours I finally managed to find the 1 thing that was different between the 2 projects.

My working example was using Play 2.4.2 while my broken App was using 2.4.1.

Here is a bare minimum example of this issue, https://github.com/narayanjr/play-s3-test

I have no idea why the issue exists but changing the Play version fixes it.

EECOLOR commented 8 years ago

Wow, good find. Thank you for reporting back. It seems to be this commit: Post binary data 'as is'.

EECOLOR commented 8 years ago

And a good reminder for me to also test with the exact play versions when reproducing bug reports.