The AWS SDK for Salesforce makes it easy for developers to access Amazon Web Services in their Apex code, and build robust applications and software using services like Amazon S3, Amazon EC2, etc. Deploy in a dev org to get started: github.com/bigassforce/aws-sdk

Amazon Simple Storage Service (S3) SDK

S3 is storage for the Internet. The Apex S3 client manipulates both buckets and their contents. Use the API methods to create and destroy objects and presign download URLs. The bucket name and object keys may be stored in custom objects etc.

Sign up then go to your AWS Console > Security Credentials > Access Keys:

String access = 'XXXXXXXXXXXXXXXXXXXX';
String secret = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY';
AwsSdk.Connector connector = new AwsSdk.Connector(access, secret);

Create a bucket:

AwsSdk.S3 s3 = connector.s3(region);
String name = 'thebucket';
s3.createBucket(name);

Add an object to a bucket:

AwsSdk.S3.Bucket bucket = connector.s3(region).bucket('thebucket');
Map<String,String> headers = new Map<String,String>{'Content-Type' => 'text/plain'};
bucket.createContent('foo.txt', headers, Blob.valueOf('bar'));

View an object:

AwsSdk.S3.Content content = connector.s3(region).bucket('thebucket').content('foo.txt');
HttpRequest request = content.presign();
String url = request.getEndpoint();

List bucket contents:

AwsSdk.Connector.Bucket bucket = connector.s3('eu-west-2').bucket('bkt');
AwsSdk.S3.ListContentsRequest request = new AwsSdk.S3.ListContentsRequest();
List<AwsSdk.S3.Content> contents = bucket.listContents(request);

Delete an object:

AwsSdk.S3.Bucket bucket = connector.s3('eu-west-2').bucket('bkt');
HttpResponse result = bucket.deleteContent('my/file.png');
System.assert(false, result); // HTTP 204 No Content

Amazon Elastic Cloud Compute (EC2) SDK

EC2 provides scalable computing capacity in the cloud. The Apex EC2 client calls services to launch instances, terminate instances, etc. The API responds synchronously, but bear in mind that the the instance state transitions take time.

Describe running instances:

AwsSdk.Ec2 ec2 = new AwsSdk.Connector(access, secret).ec2(region);
AwsSdk.Ec2.DescribeInstancesRequest request = new AwsSdk.Ec2.DescribeInstancesRequest();
ec2.describeInstances(request);

Launch a new instance:

AwsSdk.Ec2.RunInstancesRequest request = new AwsSdk.Ec2.RunInstancesRequest();
request.imageId = 'ami-08111162'; //amazon linux machine image
ec2.runInstances(request);

Terminate an existing instance:

AwsSdk.Ec2.TerminateInstancesRequest request = new AwsSdk.Ec2.TerminateInstancesRequest();
request.InstanceId = new List<String>{'i-aaaabbbb'};
ec2.terminateInstances(request);

Salesforce design considerations

When interacting with the AWS offerings, we encounter a few variations: signature vs transport, request vs response formats, REST vs SOAP oriented, etc. To maximize developer confidence, we use strongly typed DTO for request and response where appropriate. As the API footprint increases to interact with more services, we re-use several utilities:

Where supported, region-agnostic API endpoints are used to avoid proliferation of Remote Site Settings. It would be prudent to store any access keys and secret credentials in a Named Credential.

Related Posts

Last modified: 4th August 2019

Comments

Ved Kushwah 

Hi, I am trying to integrate SF with AWS and have been facing Signature mismatch issue. Can you please help me to locate at which point I am going wrong. The String To Sign generated in apex doesn’t match with the one generated by AWS and I am not sure as to where I have gone wrong. Any pointers will help me understand the issue. Thanks in Advance for the help!

Thanks very much for sharing your S3 sdk. It has helped me integrate successfully very fast. Really appreciate your work.

How to delete a file from S3?

    Construct a bucket and call deleteContent(…) on it like this:

    // credentials
    String accessKeyId = '...';
    String secretKey = '...';

    // prepare connection
    AwsSdk.Connector connector = new AwsSdk.Connector(accessKeyId, secretKey);
    AwsSdk.S3.Bucket bucket = connector.s3('eu-west-2').bucket('bkt');

    // destroy object
    HttpResponse result = bucket.deleteContent('my/file.png');
    System.assert(false, result); // HTTP 204 No Content

would you be open to adding additional examples in the documentation for the provided methods?
For example – I am struggling to implement ListContentsRequest method provided. An example
of how this works in Apex would be fantastic.
Thanks again for your fantastic work.

    Here is an example using Apex code to list the contents of an S3 bucket — best regards,

    // credentials
    String accessKeyId = '...';
    String secretKey = '...';

    // prepare connection
    AwsSdk.Connector connector = new AwsSdk.Connector(accessKeyId, secretKey);
    AwsSdk.S3.Bucket bucket = connector.s3(‘eu-west-2’).bucket(‘bkt’);

    // get list of contents
    List<AwsSdk.S3.Content> contents = bucket.listContents(new AwsSdk.S3.ListContentsRequest());
    System.assert(false, contents);

Write a Reply or Comment

Your email address will not be published.