You need to sign in to do that
Don't have an account?
Jitukawale
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.
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= Datetime.now().formatGMT('EEE,dd MMM yyyy HH:mm:ss z'); String formattedDateString = Datetime.now().format('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.setMethod(method); req.setHeader('Host','bucket.s3.amazonaws.com'); req.setEndpoint('https://bucket.s3.amazonaws.com' + '/'+ 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('Connection','keep-alive'); req.setHeader('Date', formattedDateString); req.setHeader('ACL','public-read'); req.setBody(fileString); 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; req.setHeader('Authorization',authHeader); String decoded = EncodingUtil.urlDecode(encodedStringToSign , 'UTF-8'); Http http = new Http(); try { HTTPResponse res = http.send(req); System.debug('*Resp:'+String.ValueOF(res.getBody())); if(res.getStatusCode()==200 && res.getStatus()=='OK') { String success= filename +' File has uploaded Successfully.'; //this is executing //inputFile=null; } } 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
Thanks.