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
Rahul Jain 169Rahul Jain 169 

amazon s3 integration -

I am trying to read a XML file that is stored in Amazon S3 by running this below code in anonymous block. I am getting signature does match issue. 

Here is my Code
 
public String bucketname1 = 'xmlbucketforpoc'; 
public String key1='AKIAJ4Z2VL6H3IYXAOSA';
public String secret1='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';

Datetime now = Datetime.now();

String formattedDateString1 = now.formatGmt('yyyyMMdd')+'T'+now.formatGmt('HHmmss')+'Z';
System.debug('formatted date string...'+formattedDateString1);

String canonical = 'AmazonS3'+'xmlbucketforpoc'+formattedDateString1;
String method1 = 'GET';

String filename1 = 'package1.xml';

HttpRequest req = new HttpRequest();
Http http = new Http();

req.setMethod(method1);
req.setEndpoint('https://' + bucketname1 + '.s3.us-east-1.amazonaws.com/'+ filename1);

req.setHeader('Date', formattedDateString1);
req.setHeader('Content-Type', 'application/xml');
req.setHeader('Content-Encoding', 'UTF-8');

string auth;
                
String stringToSign = 'GET' +
     '\n' +
    formattedDateString1 + '\n' +
    '/' + bucketname1 + '/' + bucketname1 + '/' + filename1;

String encodedStringToSign = EncodingUtil.urlEncode(stringToSign, 'UTF-8');
Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(stringToSign),blob.valueof(secret1));
String signed = EncodingUtil.base64Encode(mac);               

auth = 'AWS' + ' ' + key1 + ':' + signed;

req.setHeader('Authorization',auth);

HTTPResponse res = http.send(req);
System.debug('doc....'+res.getStatus());
System.debug('doc....'+res.getBody());

This is the error i get - <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>AKIAJ4Z2VL6H3IYXAOSA</AWSAccessKeyId><StringToSign>GET

Kindly help