top of page

How to store attachment/file into AWS s3 from Apex code.

Most of the time it has been observed that we are not able to store large files into Salesforce because of the storage limitation we have in Salesforce so most of the customers look for alternative option to store the files so here in this blog we are going to use AWS S3 storage to store the files from apex programming language.

Welcome to Cloudwaale blog. Best place to get step by step guide to learn new things in Salesforce. In this post we are going to store files into AWS s3 from apex code.

Step 1: Create AWS account and activate S3 storage.

Step 2: Get Secret key and access key from your AWS account as we required these things to authenticate from apex code. use following code to define variables to store access key and secret key.

String access ='**********';
String secret ='************';

Step 3: To connect with AWS S3 we have to install package in Salesforce, click here to install package in production and sandbox.

Step 4: As we have to call s3 API we have to add end point URL in remote site setting, add following URL in remote site setting and also use following code to define variable in apex code. also we have to decide on which region,bucket and folder to store the files. refer below code for same.

public String baseUrlS3 = '';
public String bucketName = 'Cloudwaale'; //Give your bucket name
public String FolderName ='Salesfoce'; // Give your folder name
public String region = 'us-east-1'; //Give your bucket region

Step 5: Fetch file form attachment and store in attach variable, refer below code.

Attachment attach = new Attachment();
attach = [select Body, ContentType, Name, parentId from Attachment WHERE limit 1];

Step 6: Now we can call S3 web service to store the file into AWS S3 use following code to authenticate API request.

 HttpResponse resp;
 String timeStamp;
 AwsSdk.Connector connector = new AwsSdk.Connector(access, secret);
 timeStamp = String.valueOf(;
 HttpResponse response;
 AwsSdk.S3.Bucket bucket;
 bucket = connector.s3(region).bucket(bucketName + '/' + FolderName);
 headers.put('Content-Type', attach.ContentType);
 response = bucket.createContent(fileName, headers, attach.Body); 
 // attach variable is coming from step 4

Step 7: we can generate the file URL so that we can access file by using URL, use following code to generate file URL

 String docUrl='';
 if (response.getStatusCode() == 200) {
 docUrl = baseUrlS3 + '/' + bucketName + '/' + FolderName + '/' + 
 System.debug('fail to update File');


That's it! this is how can store any file from apex code into AWS s3 storage and generated the URL to access the file.

To keep up with all the thing of Salesforce, customization, lightning, Apex, Visulaforce Page,REST and SOAP API and Tips and tricks to work with Salesforce head over to the Cloudwaale.

Thank you for visiting Cloudwaale, Please like and comment your view on this post.

Note: This Blog is Perspective of Author and not of Saleforce. This blog is only for study purpose.

1,848 views1 comment

Recent Posts

See All

1 Comment

Manoj Tarkar
Manoj Tarkar
Jun 30, 2021

This SDK is Updated and all methods are changed. Please change in your article.

BTW Thanks. Good Job.

bottom of page