'How to send a zipped file to S3 bucket from Apex?

Folks,

I am trying to move data to s3 from Salesforce using apex class. I have been told by the data manager to send the data in zip/gzip format to the S3 bucket for storage cost savings.

I have simply tried to do a request.setCompressed(true); as I've read it compresses the body before sending it to the endpoint. Code below:

 HttpRequest request = new HttpRequest();
    request.setEndpoint('callout:'+DATA_NAMED_CRED+'/'+URL+'/'+generateUniqueTimeStampforSuffix());
    request.setMethod('PUT');
    request.setBody(JSON.serialize(data));
    request.setCompressed(true);
    request.setHeader('Content-Type','application/json');

But no matter what I always receive this:

<Error><Code>XAmzContentSHA256Mismatch</Code><Message>The provided 'x-amz-content-sha256' header does not match what was computed.</Message><ClientComputedContentSHA256>fd31b2b9115ef77e8076b896cb336d21d8f66947210ffcc9c4d1971b2be3bbbc</ClientComputedContentSHA256><S3ComputedContentSHA256>1e7f2115e60132afed9e61132aa41c3224c6e305ad9f820e6893364d7257ab8d</S3ComputedContentSHA256>

I have tried multiple headers too, like setting the content type to gzip/zip, etc.

Any pointers in the right direction would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source