2

we are uploading files to AWS (S3 protocol) on CASE object. it's a simple call out and file gets uploaded successfully. We are storing the uploaded URL into a different custom object to display them on parent record's related list as if like actual attachment links. But I am not able to fetch the File which is supposedly happen when user clicks on that link. I have been getting below error...

System.HttpResponse[Status=Forbidden, StatusCode=403]

SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method

Here is the code snippet..Any help is greatly appreciated.

public FileDownLoad()
{ 
    HttpRequest req = new HttpRequest();
    req.setMethod(method);
    req.setHeader('Date', formattedDateString);
    req.setHeader('Authorization',createAuthHeader(method,filename ,formattedDateString ,bucketname ,key,secret));
    Set<String> getParams = new Set<String> {
    'prefix=' + EncodingUtil.urlEncode(filename+'/','UTF-8'),
    'delimiter=' + EncodingUtil.urlEncode('/', 'UTF-8')
    };

    String endpointBase = 'https://' + bucketname + '.s3.amazonaws.com/' + filename1;

    req.setEndpoint(endpointBase + '?' + 'prefix=' + EncodingUtil.urlEncode(filename+'/','UTF-8')+'&delimiter=' + EncodingUtil.urlEncode('/', 'UTF-8'));

    String stringToSign = 'PUT\n\n' +
        attach.ContentType + '\n' +
        formattedDateString + '\n' +
        '/' + bucketname + '/' + bucketname + '/' + filename;

    Http http = new Http();

    HTTPResponse res = http.send(req);
}
public string createAuthHeader(String method,String filename,String formattedDateString,String bucket,String key,String secret){
       string auth;
       String stringToSign = method+'\n\n\n'+formattedDateString+'\n/'+bucket +'/'+filename;
       Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(stringToSign),blob.valueof(secret));
       String sig = EncodingUtil.base64Encode(mac);
       auth = 'AWS' + ' ' + key + ':' + sig;
       return auth;

   }
2
  • 1
    Can you try setting your endpoint as: req.setEndpoint('https://s3.amazonaws.com/' + bucketname + '/' + filename);
    – vladykx
    Jul 13 '16 at 5:00
  • Tried., Same error. Jul 13 '16 at 18:36
5
public FileDownLoad()
...
String stringToSign = 'PUT\n\n' +
...

This doesn't make sense if you are trying to download a file. Download is GET... not PUT.

1
3

This fixed the issues. Looks like, number of '\'es caused the problem (PUT method has 3 and GET had 2).

String stringtosign = 'GET\n\n\n'+Lexpires+'\n/'+bucketname+'/'+filename;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.