+ 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 = 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());
   }  

 

PrabhaPrabha

Hi,

 

Did you get the solution. 

 

I have kind of same need here.

 

Regards

Prabhan

RajusRajus

Hi,

 

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

SaintMichaelSaintMichael

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

 

datetime expire = system.now().addYears(100);
  String formattedexpire = expire.formatGmt('yyyy-MM-dd')+'T'+
    expire.formatGmt('HH:mm:ss')+'.'+expire.formatGMT('SSS')+'Z';  

 

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

Thanks.
Hardik SFDC DevHardik SFDC Dev
You can use Drag, Drop & Upload Files to Amazon S3 App for the same (https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000B5ZPqEAN) . 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-LinkS3-Link
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.

https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000CW1OXEA1

Here is our email address. Let us know if you have any query.
support@neiloncloud.com

Thanks.