• surbhi jain 72
  • NEWBIE
  • -3 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 6
    Replies
String accessKey = 'ACCESSKEY';
            String secretKey = 'SECRETKEY';
            String bucketname = 'BUCKETNAME';
            String host = 's3-us-west-1.amazonaws.com';
            String filename = 'test1.json';
            String hashAlgo = 'HmacSHA256';
            String url = 'https://' + bucketname + '.' + host + '/' + filename;
            Date currentDate = date.today();
            DateTime currentDateRaw = DateTime.now();
            String currentDateOnly = DateTime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
            String regionName = 'us-west-1';
            String serviceName = 's3';
            String strBody = '';

            ////////////////////////////////////
            // 1 - CANONICAL REQUEST
            ////////////////////////////////////

            String strCanonicalRequest = '';
            strCanonicalRequest+='PUT\n';
            strCanonicalRequest+='\n';
            strCanonicalRequest+='content-type:application/x-www-form-urlencoded; charset=utf-8\n';
            strCanonicalRequest+='host:s3-us-west-1.amazonaws.com\n';
            strCanonicalRequest+='x-amz-date:' + currentDate + '\n';
            strCanonicalRequest+='\n';
            strCanonicalRequest+='content-type;host;x-amz-date';
            String strPayloadHash = EncodingUtil.convertToHex(Crypto.generateDigest('SHA-256', Blob.valueOf(strBody))); // Payload
            strCanonicalRequest+= '\n' + strPayloadHash.toLowerCase();

            ////////////////////////////////////
            // 2 - STRING TO SIGN
            ////////////////////////////////////

            String strStringToSign = '';
            strStringToSign+='AWS4-HMAC-SHA256\n';
            strStringToSign+=currentDateRaw + '\n';
            strStringToSign+=currentDateOnly + '/' + regionName + '/' + serviceName + '/aws4_request';
            String strCanonicalHash = EncodingUtil.convertToHex(Crypto.generateDigest('SHA-256', Blob.valueOf(strCanonicalRequest))); // Payload
            strStringToSign+= '\n' + strCanonicalHash.toLowerCase();

            ////////////////////////////////////
            // 3 - SIGNATURE
            ////////////////////////////////////

            String kSecret = 'AWS4' + secretKey;
            Blob kDate = Crypto.generateMac(hashAlgo, Blob.valueOf(currentDateOnly), Blob.valueOf(kSecret));
            Blob kRegion = Crypto.generateMac(hashAlgo, Blob.valueOf(regionName), kDate);
            Blob kService = Crypto.generateMac(hashAlgo, Blob.valueOf(serviceName), kRegion);
            Blob kSigning = Crypto.generateMac(hashAlgo, Blob.valueOf('aws4_request'), kService);
            String strSignature = EncodingUtil.convertToHex(Crypto.generateMac(hashAlgo, Blob.valueOf(strStringToSign), kSigning));
            strSignature = strSignature.toLowerCase();

            ////////////////////////////////////
            // 4 - AUTHORIZATION HEADER
            ////////////////////////////////////

            String strAuthorizationHeader = 'AWS4-HMAC-SHA256 ';
            strAuthorizationHeader+= 'Credential=' + accessKey + '/' + currentDateOnly + '/' + regionName + '/' + serviceName + '/aws4_request, ';
            strAuthorizationHeader+= 'SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, ';
            strAuthorizationHeader+= 'Signature=' + strSignature;


            // PUT
            Http http = new Http();
            HttpRequest req = new HttpRequest();
            req.setEndPoint(url);
			req.setMethod('PUT');
			req.setBody(body);
			req.setHeader('ACL', 'public-read-write');
            req.setHeader('Authorization', strAuthorizationHeader);
			req.setHeader('Date', currentDateOnly);
			req.setHeader('x-amz-content-sha256', 'UNSIGNED-PAYLOAD');
            HttpResponse response = http.send(req);

            if (response.getStatusCode() != 200) {
                System.debug('The status code returned was not expected: ' +
                        response.getStatusCode() + ' ' + response.getStatus());
                System.debug('Auth: ' + strAuthorizationHeader);

            else {

                Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                System.debug('This is working');
                System.debug(response.getBody());
                String output =  (String)results.get('result');


            }

Hello,

I'm in the process of connecting to S3 out of APEX and I'm receiving the following error message when executing an anonymous request:

The authorization header is malformed; the authorization header requires three components: Credential, SignedHeaders, and Signature.

Example of Signature:
Auth: AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/Tue, 09 Jul 2019 07:54:14 GMT/us-west-1/s3/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, Signature=fe5f80f77d5fa3beca038a248ff027d04453421fe2855ddc963176630326f1024

When I look at the authorization signature, it looks to be setup correctly.  I"m able to add that signature to the body of my postman request and I get a successful response but when I add it as a header, I receive a 403 message.

Has any one experienced something similar?  If so, any assistance woiuld be greatly appreciated.  TY!

 
I am trying to upload files from a lightning component to AWS directly with saving in salesforce. I get 400 Bad Request error.
Can anyone help?

 
public with sharing class TestingAmazon {
    @AuraEnabled
    
    public static String testIntegration(String filename, String base64Data , String contentType) {
        System.debug('filename---->>'+filename);
        System.debug('base64Data---->>'+base64Data);
        System.debug('contentType---->>'+contentType);
        //Blob beforeblob = EncodingUtil.base64Decode(base64Data);
        Blob beforeblob = Blob.valueOf(base64Data);
        String attachmentBody = EncodingUtil.base64Encode(beforeblob);
        String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
        String key = '-------------------------------';
        String secret = '---------------------------------------------';
        String bucketname = 'testfileupoad';
        String host = 's3.ap-south-1.amazonaws.com';
        String method = 'PUT';

        HttpRequest req = new HttpRequest();
        req.setMethod(method);
        req.setEndpoint('https://' + bucketname + '.' + host + '/' + bucketname + '/' + filename);

        req.setHeader('Host', bucketname + '.' + host);
        req.setHeader('Content-Length', String.valueOf(attachmentBody.length()));
        req.setHeader('Content-Encoding', 'base64');
        req.setHeader('Content-type', contentType);
        req.setHeader('Connection', 'keep-alive');
        req.setHeader('Date', formattedDateString);
        req.setHeader('ACL', 'public-read');
        req.setBodyAsBlob(beforeblob);
        System.debug('req---->'+req);
        
       /* String stringToSign = 'PUT\n\n' +
                contentType + '\n' +
                formattedDateString + '\n' +
                '/' + bucketname + '/' + bucketname + '/' + filename; */

        String stringToSign = 'PUT\n' +
                contentType + '\n' +
                formattedDateString + '\n' +
                '/' + bucketname + '/' + bucketname + '/' + filename;
        System.debug('stringToSign---->>'+stringToSign);

        String encodedStringToSign = EncodingUtil.urlEncode(stringToSign, 'UTF-8');
        Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(stringToSign),blob.valueof(secret));
        String signed = EncodingUtil.base64Encode(mac);
        String authHeader = 'AWS' + ' ' + key + ':' + signed;
        System.debug('authHeader---->>'+authHeader);
        req.setHeader('Authorization',authHeader);
        String decoded = EncodingUtil.urlDecode(encodedStringToSign , 'UTF-8');

        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug('RESPONSE STRING: ' + res.toString());
        System.debug('RESPONSE STATUS: ' + res.getStatus());
        System.debug('STATUS_CODE: ' + res.getStatusCode());
        if(res.getStatusCode() == 200){
            String ReturnUrl = 'https://'+host+'/'+bucketname+'/'+bucketname+'/'+filename;
            System.debug('ReturnUrl---->>'+ReturnUrl);
            return ReturnUrl;
        }
        return 'ERROR';
    }
}

 
Hello! Does anyone know a ML AI chatbot with easy integration to Salesforce?

I've two salesforce accounts(i.e say A & B), with two different email id's. When i send Connection invite from A to B, B is receiving the Email, But when i send it from B, A is not receiving any invites. Note: B has not accepted the invite sent by  A.

 

All the settings remain same in both the accounts, when i checked.

 

I've tested the deliverability also. A is getting 52 test emails, but B is getting 51 test emails.

 

Please suggest me the way to proceed or please let me know in case any other specific settings need to be checked..

  • March 30, 2012
  • Like
  • 0

Has anyone ever used Salesforce to Salesforce within their own organization?  We are considering it, and I would like to hear some of the pros and cons.

 

 

Thanks, 

 

Stephen

sgoldber@redhat.com 

Hi,

I want to do salesforce certification Dev 401 and DEV 501. We are partners of Salesforce, so can anybody tell me the exact price of the certification ?

Any pointers on these is highly appreciated.

Thank You,
Yash