An exception “The Content-MD5 you specified did not match what we received”

后端 未结 5 1652
谎友^
谎友^ 2021-01-17 17:14

I got an exception, I never got before when testing my application that uploads a file from ec2 to s3. The content is:

Exception in thread \"Thread-1\" com.a         


        
5条回答
  •  自闭症患者
    2021-01-17 17:43

    FWIW, I've managed to find a completely different way of triggering this problem, which requires a different solution.

    It turns out that if you decide to assign ObjectMetadata to a PutObjectRequest explicitly, for example to specify a cacheControl setting, or a contentType, then the AWS SDK mutates the ObjectMetadata instance to stash the MD5 that it computes for the put request. This means that if you are putting multiple objects, all of which you think should have the same metadata assigned to them, you still need to create a new ObjectMetadata instance for each and every PutObjectRequest. If you don't do this, then it reuses the MD5 computed from the previous put request and you get the MD5 mismatch error on the second object you try to put.

    So, to be explicit, doing something like this will fail on the second iteration:

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentType("text/html");
    for(Put obj: thingsToPut)
    {
        PutObjectRequest por = 
            new PutObjectRequest(bucketName, obj.s3Key, obj.file);
        por = por.withMetadata(metadata);
        PutObjectResult res = s3.putObject(por);
    }
    

    You need to do it like this:

    for(Put obj: thingsToPut)
    {
        ObjectMetadata metadata = new ObjectMetadata(); // <<-- New ObjectMetadata every time!
        metadata.setContentType("text/html");
        PutObjectRequest por =
            new PutObjectRequest(bucketName, obj.s3Key, obj.file);
        por = por.withMetadata(metadata);
        PutObjectResult res = s3.putObject(por);
    }
    

提交回复
热议问题