We have a method that puts files in an AWS S3 Bucket, but since Spring 22 was applied to an env it has been failing with 'System.CalloutException: Script-thrown exception'
We believe it to be related to this Known-Issue: https://trailblazer.salesforce.com/issues_view?id=a1p4V000001JitQQAS&title=script-thrown-exception-in-apex-callouts
The workaround suggested is about content encoding of the response, but that is not something we can control with the AWS S3 bucket response to my knowledge.
Is there ANYTHING we can do within the Salesforce code to fix this issue?
public Boolean putFile(String filename, String contentType, Blob fileBlob) {
HttpRequest req = new HttpRequest();
String endPoint = 'callout:AWSS3Bucket/' + filename;
req.setEndpoint(endPoint);
req.setTimeout(TIMEOUT);
req.setMethod('PUT');
req.setHeader('Connection', 'keep-alive');
req.setHeader('Content-Length', String.valueOf(EncodingUtil.base64Encode(fileBlob).length()));
req.setHeader('Content-Encoding', 'UTF-8');
if (String.isEmpty(contentType)) {
// AwS Defaults to CONTENT_TYPE_BINARY if not specified, setting explicitly to avoid confusion
req.setHeader('Content-Type', CONTENT_TYPE_BINARY);
} else {
req.setHeader('Content-Type', contentType);
}
req.setBodyAsBlob(fileBlob);
Http http = new Http();
HttpResponse resp = http.send(req);
if (resp != null && resp.getStatusCode() == 200) {
return true;
} else {
System.debug('Status: ' + resp.getStatus());
System.debug('Body: ' + JSON.serializePretty(resp.getBody()));
return false;
}
}