Using Named Credentials to Integrate Salesforce with Amazon S3

NEW from the Tether Tips Blog – Integrating Salesforce with Amazon S3

A really helpful tip from our Salesforce Developer Kunal Jangid this week.

Introduction:

Data Storage is one of the most important factors to consider while dealing with cloud solutions. In Enterprise, Performance and Unlimited Editions, Salesforce offers up to 2 GB of file storage per user license. Storage limit is not restricted and can be increased by contacting the Salesforce support, but there are other options, such as the Amazon S3.


Amazon S3 is an object-based storage service that offers high level of security, performance and scalability. It is also one of the cheapest cloud storage options available. Therefore, integrating Salesforce with Amazon S3 enables businesses to overcome the issue of limited storage in cases such as archiving critical historical data including files, images or any other attachment related to the org.

In this blog, I will show you an easy way to integrate your Salesforce Org with Amazon S3 using Named Credentials. Named Credentials is a configuration feature provided by Salesforce that takes care of the authentication process while integrating with third-party services. It makes a developer’s life much easier, as one does not have to worry about the issue of a reference token getting expired or adding credentials to the remote site settings. Salesforce manages all authentication for callouts that specify a named credential as the callout endpoint.

Alright, let us dig-in straightaway and have a look at the steps to take in order to make our integration work.

Steps:

  • Go to the AWS Management Console of the account that we need to integrate with.
  • Grab the Access Key Id and the Secret Access Key from the ‘My Security Credentials’ sections available under the drop-down menu button on the top right corner labelled by your account’s name.
AWS Management Console
  • Log into your Org and access “Named Credentials” from the Setup menu
Enter named credentials in the Quickfind in Setup
  • Create a new named credential. Input the values for the label and the name (used to refer to the named credential via API) fields.
  • The URL to enter in the URL field must follow the following pattern :- (scheme)://(instance_name).(region_name).amazonaws.com/

In our case it will look something like :-https://S3.us-east-1.amazonaws.com/

  • Under the Authentication Section choose ‘Named Principal’ as the Identity type and ‘AWS Signature Version 4’ as the Authentication Protocol. This will make four more fields visible below where the value for AWS Access Key Id, AWS Secret Access Key,  AWS Region and AWS Service must be entered. Once all the fields are filled-in, your named credentials detail section must be looking something like this :-
  • Our named credential is set-up perfectly for integration now and we can test that by using the Developer Console. Open the Execute Anonymous Window from the Developer Console and type in the following code: –

HttpRequest request = new HttpRequest();
request.setEndpoint(‘callout:S3_Integration/’);
request.setMethod(‘GET’);
request.setHeader(‘Content-Type’,’application/xml’);
Http http = new Http();
HTTPResponse response = http.send(request);
System.debug(response.getBody());
System.debug(response.getStatus());

In the above code, we are simply creating a new request from within Salesforce and setting the values for the required request parameters to make the integration. Do remember that, in the setEndpoint() method, ‘S3_Integration’ is the name given to the named credential. Once our request is ready, we create a new instance of the Http object and use the send() method to send our request to AWS. As you can see from the screenshot below, once the code is executed the response returned by AWS contains details of all the S3 Buckets available in an XML Format.

Now that the connection to AWS is finally established, we can simply use the standard HTTP methods such as GET, to retrieve information regarding the buckets or PUT, to post org data such as files, images or any other attachments to the S3 bucket. You can go ahead and have a look at all the REST API methods provided by Amazon for S3 here : https://docs.aws.amazon.com/AmazonS3/latest/API/Welcome.html

Here you go it was as simple as that! Enjoy unlimited amounts of data backup and storage at super cheap costs.

We hope this helps, and thanks for reading!

#salesforce #salesforcecrm #tethertips #crm

Share the Post: