I am trying to call AWS S3 DeleteObjects REST API from Apex. I am able to successfully perform this operation in Postman using their Sig4 Auth tool but I am struggling to get this working in Apex.
I have tried a few different approaches,
1.) using Named Credentials (Focus of this question)
2.) using sfdcfox's AWS class (referenced in the answer here.)
For both of these approaches I was able to get basic GET and PUT functionality working but the POST seems to require a different approach. This question is focused on the Named Credential approach as I feel it will be the simplest solution. If I am unable to do this using Named Credentials, I will ask a separate question with more details on approach #2.
This POST method requires a content-MD5 header which is generated from the body of the request. By default, the AWS Sig4 Named Credential does not have this header when working in Apex. After I set the body, I generate the content-md5 header, add it to the request, and send the request. This generates an error saying the following:
There were headers present in the request which were not signed
Here is the code performing the callout:
HttpRequest req = new HttpRequest();
String body = '<Delete ><Object><Key>test.png</Key></Object><Object><Key>test1.png</Key></Object></Delete>';
Blob targetBlob = Blob.valueOf(body);
Blob md5checksum = Crypto.generateDigest('MD5',targetBlob);
string ContentMD5 = EncodingUtil.base64Encode(md5checksum);
req.setMethod('POST');
req.setBody(body);
req.setHeader('content-md5', ContentMD5);
req.setEndpoint('callout:aws/?delete');
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug(res.getBody());
Here is a screenshot of the Named Credential config:
<Delete >
and required XML namespace is missing from the<Delete>
element. The bad XML would have resulted in a different error, you didn't get that far.