Hello readers, In this tutorial, I am going to show you how to Upload Files on Amazon s3 bucket through Salesforce Apex. An Amazon S3 bucket is a public cloud storage resource available in Amazon Web Services.

What is Amazon s3 bucket?

An Amazon S3 bucket is a public cloud storage resource available in Amazon Web Services’ (AWS) Simple Storage Service (S3), an object storage offering. Amazon S3 buckets, which are similar to file folders, store objects, which consist of data and its descriptive metadata.

With the help of the following code, you can upload file on Amazon S3 through Apex

Prerequisites

  • Need account on AWS.
  • Create a bucket on AWS.
  • Bucket Name, Secret key, key, and the region is used for authentication

1- Create an Account:

Create an Account with the required details and Login to the Account.

2- Create a bucket on AWS.

A- After login to your AWS account. Goto Services and choose s3 as shown in the image.

Amazon s3 bucket

B- Click on the Create bucket button as shown in the image.

Amazon s3 bucket

C- Enter a Unique name, choose Reason and then click on the Next button.

Amazon s3 bucket

D- The following screen will appear. Click on the Next button.

Amazon s3 bucket

E-Click on the Next button.

Amazon s3 bucket

F- The next screen that will appear will look like this.

Amazon s3 bucket

Choose Create bucket button.

You can see your bucket is created.

Amazon s3 bucket

Where is Access Key and Secret Key?

You can find your Access Key and Secret Key by going to the following location as shown in the below image.

Amazon s3 bucket

Expand the Access keys (access key ID and secret access key) panel and copy Access and Secret Key.

Now Login to your Salesforce Account and write the following code into your apex class.

Attachment attach = [select Id,Body,
ContentType,
Name
from Attachment

limit 1
];
 
String attachmentBody = EncodingUtil.base64Encode(attach.Body);
String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z');
String key = '*******************';
String secret = '**********************************';
String bucketname = 'test-s3-service';
String host = 's3-us-west-1.amazonaws.com'; 
String method = 'PUT';
String filename = attach.Id + '-' + attach.Name;
 
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', 'UTF-8');
req.setHeader('Content-type', attach.ContentType);
req.setHeader('Connection', 'keep-alive');
req.setHeader('Date', formattedDateString);
req.setHeader('ACL', 'public-read');
req.setBody(attachmentBody);
 
String stringToSign = 'PUT\n\n' +
attach.ContentType + '\n' +
formattedDateString + '\n' +
'/' + bucketname + '/' + bucketname + '/' + filename;
 
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;
req.setHeader('Authorization',authHeader);
String decoded = EncodingUtil.urlDecode(encodedStringToSign , 'UTF-8');
 
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('*Resp:' + String.ValueOF(res.getBody()));
System.debug('RESPONSE STRING: ' + res.toString());
System.debug('RESPONSE STATUS: ' + res.getStatus());
System.debug('STATUS_CODE: ' + res.getStatusCode());
 
 

Run the following code and verify by going to your aws account.

View More:

Conclusion:

I hope you understand how to upload files from Salesforce to Amazon s3 bucket. Please don’t hesitate to comment for any technical help.

Thank You.

Leave a Comment