function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

AWS Salesforce Integration

Hi All,

I am writing a simple request to upload the file to AWS. I have verified different my calculated signature but still getting an error message in the callout. Refer below code to verify.
public static void SendRequest(){    
        String fileString='fileString';
        String key='xxxxxxxxxxxxxxx';
        String secret='xxxxxxxxxxxxxxxxxxxxxxxx';
        //String formattedDateString='EEE,dd MMM yyyy HH:mm:ss z');
        String formattedDateString ='EEE, dd MMM yyyy HH:mm:ss z','America/Los_Angeles');
        String bucketname = 'bucket';
        String method = 'PUT';
        String filename = 'MyFile';
        HttpRequest req = new HttpRequest();
        req.setEndpoint('' + '/'+ bucketname + '/' + filename);
        req.setHeader('Content-Length', string.valueOf(fileString.length()));
        req.setHeader('Content-Encoding', 'UTF-8');
    //  req.setHeader('Content-Disposition', 'attachment'); 
        req.setHeader('Content-type', 'application/json');        
        req.setHeader('Date', formattedDateString);
        String stringToSign ='PUT\n\n'+'application/json\n'+formattedDateString+'\n/'+bucketname+'/'+bucketname+'/'+filename;
        String encodedStringToSign = EncodingUtil.urlEncode(stringToSign,'UTF-8');        
        String signed = createSignature(stringToSign,secret);
        String authHeader = 'AWS' + ' ' + key + ':' + signed;
        String decoded = EncodingUtil.urlDecode(encodedStringToSign , 'UTF-8');        
        Http http = new Http();
        try {
             HTTPResponse res = http.send(req);          
             if(res.getStatusCode()==200 && res.getStatus()=='OK')
                 String success= filename +' File has uploaded Successfully.';
//this is executing 

            } catch(System.CalloutException e) {

   // return ApexPages.Currentpage();


    public static string createSignature(string canonicalBuffer,String secret){
        string sig;
        Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(canonicalBuffer),blob.valueof(secret));
        sig = EncodingUtil.base64Encode(mac);  
        return sig;

When I perform callout I am getting a response:

<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><AWSAccessKeyId>kdjkhkghkjhkhdkhkdhkhfkh</AWSAccessKeyId><StringToSign>PUT