You need to sign in to do that
Don't have an account?
Michael M
Apex 400 error: XAmzContentSHA256Mismatch, The provided 'x-amz-content-sha256' header does not match what was computed [closed]
Hello, I am working on "PUTting" files attached to my Lead records into an s3 bucket. Some of the files get neatly placed in the s3 bucket, however when there are multiple files, I get a 200 response on the some of them but a 400 response on the rest of them, stating The provided 'x-amz-content-sha256' header does not match what was computed.. Any idea how I can fix this?
My code is here:
public class S3Controller {
public static void UploadDocToS3Server(string recordId)
{
UploadDocument(recordId);
}
@future(callout=true)
public static void UploadDocument(string recordId)
{
String key = '123';
String secret = '123+123+123';
String bucket = '123';
String host = 's3-us-east-1.amazonaws.com';
String method = 'PUT';
ProfilityCallout service=new ProfilityCallout(key,secret,bucket,method,host);
service.UploadDocuments(recordId);
}
}
public class ProfilityCallout {
public string awsKey {get;set;}
public string awsSecret {get;set;}
public string bucketName {get;set;}
public string methodName {get;set;}
public string hostName {get;set;}
public ProfilityCallout(string key, string secret, string bucket, string method, string host) {
awsKey=key;
awsSecret=secret;
bucketName=bucket;
methodName=method;
hostName=host;
}
public static string ContentType(string fileType) {
switch on fileType.toLowerCase() {
when 'docx'
{
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
}
when 'csv'
{
return 'application/vnd.ms-excel';
}
when 'wav'
{
return 'audio/wav';
}
when 'wmv'
{
return 'video/x-ms-wmv';
}
when 'mp3'
{
return 'audio/mpeg';
}
when 'mp4'
{
return 'video/mp4';
}
when 'png'
{
return 'image/png';
}
when 'pdf'
{
return 'application/pdf';
}
when else {
return 'image/jpeg';
}
}
}
public void UploadDocuments(string recordId) {
Lead ref = [select id, mrn__c,Admit_Date__c, referral_account__r.name,Hosp_Admit_Date_new_formula__c, facility__r.name from lead where id = : recordId];
string referralid;
string mrn;
string hosp;
string admitdate;
string facility;
if (ref.MRN__c != null){
mrn = ref.MRN__c;
}
else{
mrn = 'noMrnListed';
}
if (ref.Referral_Account__c != null){
hosp = ref.Referral_Account__r.name.replaceAll( '', '').replaceAll( ' ', '');
}
else{
hosp = 'noHospitalListed';
}
if (ref.Admit_Date__c != null){
admitdate = ref.Hosp_Admit_Date_new_formula__c;
}
else{
admitdate = 'admitDateMissing';
}
List<ContentDocumentLink> links=[SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId=:recordId];
Set<Id> ids=new Set<Id>();
for(ContentDocumentLink link:links)
{
ids.add(link.ContentDocumentId);
}
List<ContentVersion> versions=[SELECT VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :ids AND IsLatest = true];
for(ContentVersion attach:versions)
{
String attachmentBody = EncodingUtil.base64Encode(attach.VersionData);
String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
String filename = attach.Title;
string contentType=ContentType(attach.FileExtension);
HttpRequest req = new HttpRequest();
req.setMethod('PUT');
req.setEndpoint('callout:Profility/'+ filename.replaceAll( '\\s+', '')+'.'+attach.FileExtension.toLowerCase() +'~'+ref.id + '~'
+ mrn + '~' + hosp + '~'+ admitdate + '~' + ref.Facility__r.name.replaceAll( '', '').replaceAll( ' ', '')
+ '.'+attach.FileExtension.toLowerCase());
Blob pdfBlob = EncodingUtil.base64Decode(attachmentBody);
req.setBodyAsBlob(pdfBlob);
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug(versions.size() + 'FILES ATTACHED');
system.debug('>>>> The Request');
system.debug(req);
system.debug('>>>> The response');
system.debug(res);
system.debug('>>>> the body');
system.debug(res.getbody());
system.debug('>>>> status code');
system.debug(res.getstatuscode());
}
}
}
My code is here:
public class S3Controller {
public static void UploadDocToS3Server(string recordId)
{
UploadDocument(recordId);
}
@future(callout=true)
public static void UploadDocument(string recordId)
{
String key = '123';
String secret = '123+123+123';
String bucket = '123';
String host = 's3-us-east-1.amazonaws.com';
String method = 'PUT';
ProfilityCallout service=new ProfilityCallout(key,secret,bucket,method,host);
service.UploadDocuments(recordId);
}
}
public class ProfilityCallout {
public string awsKey {get;set;}
public string awsSecret {get;set;}
public string bucketName {get;set;}
public string methodName {get;set;}
public string hostName {get;set;}
public ProfilityCallout(string key, string secret, string bucket, string method, string host) {
awsKey=key;
awsSecret=secret;
bucketName=bucket;
methodName=method;
hostName=host;
}
public static string ContentType(string fileType) {
switch on fileType.toLowerCase() {
when 'docx'
{
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
}
when 'csv'
{
return 'application/vnd.ms-excel';
}
when 'wav'
{
return 'audio/wav';
}
when 'wmv'
{
return 'video/x-ms-wmv';
}
when 'mp3'
{
return 'audio/mpeg';
}
when 'mp4'
{
return 'video/mp4';
}
when 'png'
{
return 'image/png';
}
when 'pdf'
{
return 'application/pdf';
}
when else {
return 'image/jpeg';
}
}
}
public void UploadDocuments(string recordId) {
Lead ref = [select id, mrn__c,Admit_Date__c, referral_account__r.name,Hosp_Admit_Date_new_formula__c, facility__r.name from lead where id = : recordId];
string referralid;
string mrn;
string hosp;
string admitdate;
string facility;
if (ref.MRN__c != null){
mrn = ref.MRN__c;
}
else{
mrn = 'noMrnListed';
}
if (ref.Referral_Account__c != null){
hosp = ref.Referral_Account__r.name.replaceAll( '', '').replaceAll( ' ', '');
}
else{
hosp = 'noHospitalListed';
}
if (ref.Admit_Date__c != null){
admitdate = ref.Hosp_Admit_Date_new_formula__c;
}
else{
admitdate = 'admitDateMissing';
}
List<ContentDocumentLink> links=[SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId=:recordId];
Set<Id> ids=new Set<Id>();
for(ContentDocumentLink link:links)
{
ids.add(link.ContentDocumentId);
}
List<ContentVersion> versions=[SELECT VersionData,Title,ContentDocumentId,FileExtension FROM ContentVersion WHERE ContentDocumentId = :ids AND IsLatest = true];
for(ContentVersion attach:versions)
{
String attachmentBody = EncodingUtil.base64Encode(attach.VersionData);
String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
String filename = attach.Title;
string contentType=ContentType(attach.FileExtension);
HttpRequest req = new HttpRequest();
req.setMethod('PUT');
req.setEndpoint('callout:Profility/'+ filename.replaceAll( '\\s+', '')+'.'+attach.FileExtension.toLowerCase() +'~'+ref.id + '~'
+ mrn + '~' + hosp + '~'+ admitdate + '~' + ref.Facility__r.name.replaceAll( '', '').replaceAll( ' ', '')
+ '.'+attach.FileExtension.toLowerCase());
Blob pdfBlob = EncodingUtil.base64Decode(attachmentBody);
req.setBodyAsBlob(pdfBlob);
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug(versions.size() + 'FILES ATTACHED');
system.debug('>>>> The Request');
system.debug(req);
system.debug('>>>> The response');
system.debug(res);
system.debug('>>>> the body');
system.debug(res.getbody());
system.debug('>>>> status code');
system.debug(res.getstatuscode());
}
}
}
Based on the error message, it does not appear to be a Salesforce-related issue. I found few relevant posts that might help to get started.
https://community.n8n.io/t/xamzcontentsha256mismatch-when-passing-more-than-one-item-to-the-aws-s3-node/2582
https://github.com/aws/aws-sdk-ruby/issues/1594
For more inputs, please consider posting your ask on the above Git link as a "new issue" for better visibility.
If this information helps, please mark the answer as best. Thank you
All Answers
Based on the error message, it does not appear to be a Salesforce-related issue. I found few relevant posts that might help to get started.
https://community.n8n.io/t/xamzcontentsha256mismatch-when-passing-more-than-one-item-to-the-aws-s3-node/2582
https://github.com/aws/aws-sdk-ruby/issues/1594
For more inputs, please consider posting your ask on the above Git link as a "new issue" for better visibility.
If this information helps, please mark the answer as best. Thank you
THank you!