function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tu DaoTu Dao 

How to Upload an Attachment to Amazon S3 using REST Interface

Hi all,


I implemented a method to upload an Attachment to Amazon S3 but I'm having this issue : AWS authentication requires a valid Date or x-amz-date header


Any help is appreciated.


Attachment attachment = [Select Id, ContentType, Body, BodyLength from Attachment limit 1];
      //Set<Id> ids = new Map<Id,Attachment>(attachments).keySet();
      AWSKeys credentials = new AWSKeys('Keys');
      S3.AmazonS3 as3 = new S3.AmazonS3(credentials.key,credentials.secret);
      Datetime now =;
      String formattednow = now.formatGmt('yyyy-MM-dd')+'T'+now.formatGmt('HH:mm:ss')+'.'+now.formatGMT('SSS')+'Z';
      String stringtosign = 'PUT \n\n';
      stringtosign += attachment.ContentType + '\n';
      stringtosign += formattednow + '\n';
      stringtosign += '/mybucket/' + attachment.Id;
      Blob mac = Crypto.generateMac('HMacSHA1', blob.valueof(stringtosign),blob.valueof(as3.secret)); 
      String signature = EncodingUtil.base64Encode(mac);
      HttpRequest req = new HttpRequest(); 
     //Set HTTPRequest Method
     //Set HTTPRequest header properties
     req.setHeader('content-type', attachment.ContentType);
     req.setHeader('Date', formattednow);
     req.setHeader('Authorization', 'AWS ' + as3.key + ':' + signature);
      try {
            Http http = new Http();
           //Execute web service call here      
           HTTPResponse res = http.send(req);
   } catch(System.CalloutException e) {
      System.debug('===================EXCEPTION ' + e.getMessage() + e.getStackTraceString());





Did you get the solution. 


I have kind of same need here.







I have similar kind of issue.Did u able to solve this issue.


I created a date that is 100 years into the future. I don't want the uploaded file to expire.


datetime expire =;
  String formattedexpire = expire.formatGmt('yyyy-MM-dd')+'T'+


Maxi SMaxi S
Hi Dao,

I want to implement same kind of requirement.

Can you please provide me a steps to implement above method to upload an attachment to Amazon S3

Hardik SFDC DevHardik SFDC Dev
You can use Drag, Drop & Upload Files to Amazon S3 App for the same ( . I am using it from last 2 years and it has got following features
  1. Drag and Drop interface to upload file
  2. You can view/delete your files from Salesforce
  3. Its free
Also they provide Good Support if you have any issues & they provide additional customizations.
S3- Link is FREE App for Salesforce - Amazon S3 Connector. Its also available on Appexchange. 

    Attach file related to any Salesforce object on Amazon.
    Ulimited free storage for one year.
    Multiple file uplaod.
    No file size limit for upload.
    File access control capabiliy.
    Track file downloads by users.
    File exlorer capability.

Here is our email address. Let us know if you have any query.