• Nitin Rawat 9
  • NEWBIE
  • 0 Points
  • Member since 2016
  • Salesforce Developer
  • DMI Finance

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 2
    Replies

Hi, 

While connecting my VS code with my Org I am getting this error message: 

Ended with error spawn C:\Windows\system32\cmd.exe ENOENT

I have already given the Path and  checked the Proxy and Firewall. 
Please help me on this. 

 

Thanks in Advance!!

Hi,

I am getting "status code is 200" pass when I test it through POSTMAN. I am also able to see my uploaded file on my s3 bucket folder. This is the endpoint I am using "https://personalsfbucket.s3.ap-south-1.amazonaws.com/DocumentUploaded/Demo". Now when I use the same endpoint in my Code : 
req.setEndpoint('https://' + bucketname + '.' + host + '/' + foldername + '/' + fileName);

I am getting "System.HttpResponse[Status=Bad Request, StatusCode=400]".

Any Help or suggestion!!!!

For your reference here's my code: 

public with sharing class AWS_File_Upload_Ctrl {

    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 = 'AKIAJNWEGTHJFMQQLPBA';
        String secret = '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';
        String bucketname = 'personalsfbucket';
        String foldername = 'DocumentUploaded';
        String host = 's3.ap-south-1.amazonaws.com';
        String method = 'PUT';

        HttpRequest req = new HttpRequest();
        req.setMethod(method);
        req.setEndpoint('https://' + bucketname + '.' + host + '/' + foldername + '/' + fileName); //('https://' + host + '/' + bucketname + '/' + filename);
        req.setTimeout(120000);
        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';
    }
}

What I am trying to do is uploading file to s3 bucket of AWS directly without saving it in SF.

Please do let me know what/where I am doing wrong and provide any helpful links.

Thanks in advance!!

Hi, 

This is my code to upload File to s3 bucket.

public with sharing class AWS_File_Upload_Ctrl {

    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 = 'AKIAJNWEGTHJFMQQLPBA';
        String secret = '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';
        String bucketname = 'personalsfbucket';
        String foldername = 'DocumentUploaded';
        String host = 's3.ap-south-1.amazonaws.com';
        String method = 'PUT';

        HttpRequest req = new HttpRequest();
        req.setMethod(method);
        req.setEndpoint('https://' + bucketname + '.' + host + '/' + fileName); //('https://' + host + '/' + bucketname + '/' + filename);
        req.setTimeout(120000);
        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';
    }
}

I am getting error message while runing it through Anonymous Window:

System.HttpResponse[Status=Bad Request, StatusCode=400]

Please Help:(

 

Thanks in advance:)

Hello Everyone,

I am getting this error message:

"sObject type:AuraDefinitionBundle'is not supported".

any workaround and solution for this?

Thanks in advance!!

Hi,

I have "EmailStatus" object in which there is a field named as "TimesOpened" which counts to "1" when email is read. So I want to update  "Email Opt Out" checkbox whenever this  field "TimesOpened" counts to "1".
"Email Opt Out" is Checkbox on Lead object.

Thanks in Advance!!

Hello Everyone,

I am getting this error message:

"sObject type:AuraDefinitionBundle'is not supported".

any workaround and solution for this?

Thanks in advance!!

I need to upload a csv file from Apex to a global bucket in s3 and keep the URL.

Below described is my approach for the same using AWS request signing process.
 
public static void uploadCSVFileToS3(String csvFile, String filename, String awsAccessKey, String awsSecretKey, String bucketname){
        try{
            String formattedDateString = Datetime.now().format('EEE, dd MMM yyyy HH:mm:ss');
            String requestType = 'PUT';
            String contentType = 'text/csv';
            String filePath = 'https://s3.amazonaws.com' + '/'+ bucketname + '/' + 'demo.csv';
            
            HttpRequest req = new HttpRequest();
            req.setMethod(requestType);
            req.setHeader('Host','https://s3.amazonaws.com');
            req.setEndpoint(filePath);
            req.setHeader('Content-Length', String.valueOf(csvFile.length()));
            req.setHeader('Content-Type', contentType);
            req.setHeader('Date', formattedDateString);
            req.setHeader('ACL', 'public-read-write');
            Blob CSV = Blob.valueof(csvFile);
            req.setBodyAsBlob(CSV);
            
            String auth = createAuthHeader(requestType, contentType, filename, formattedDateString, bucketname, awsAccessKey, awsSecretKey);
            
            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());
        } catch(System.CalloutException e) {
            system.debug('AWS Service Callout Exception: ' + e.getMessage());
            system.debug('AWS Service Callout Exception: ' + e.getCause());
            system.debug('Exception: ' + e.getStackTraceString());
        } catch(Exception e) {
            system.debug('Exception: ' + e.getMessage());
            system.debug('Exception: ' + e.getStackTraceString());
        }  
    }

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

I have followed the link here (https://developer.salesforce.com/forums/?id=906F0000000BMDFIA4) for the approach used.

I am able to upload the file to same bucket from ruby or javascript using the aws sdk's, but it is giving me a response code of 400 (Bad Request).
I think this is a problem of the process of signing request.

It will be highly appreciated if someone can help me here.