• Hardik SFDC Dev
  • NEWBIE
  • -1 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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 = 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
     req.setMethod('PUT');
   
     //Set HTTPRequest header properties
     req.setHeader('content-type', attachment.ContentType);
     req.setHeader('Content-Length',String.valueOf(attachment.BodyLength));
     req.setHeader('Date', formattednow);
     req.setHeader('Authorization', 'AWS ' + as3.key + ':' + signature);
     req.setEndpoint('http://s3.amazonaws.com');
     
      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());
   }  

 

  • December 12, 2012
  • Like
  • 0