Showing posts with label Apex. Show all posts
Showing posts with label Apex. Show all posts

Friday, 11 January 2013

Amazon S3 signature for Apex

Hi,

Please find below the method to return a valid signature generated in Salesforce's Apex language.
This signature is required to make any Rest/Soap call on Amazon's S3 servers.

Arguments to this method are : Current DateTime and the name of Operation that you want to perform for example "Get Object".

 public static String getSignature(DateTime now,String op){
       
        //format should be like 2006-01-01T12:00:00.000Z
        String formattednow = now.formatGmt('yyyy-MM-dd')+'T'+now.formatGmt('HH:mm:ss')+'.'+now.formatGMT('SSS')+'Z';
        System.Debug('Formatted date : '+formattednow);
       
        String canonical = 'AmazonS3'+op+formattednow; //"AmazonS3" + OPERATION + Timestamp
       
        System.debug('CANONICAL = '+canonical);
       
        Blob bsig = Crypto.generateMac('HmacSHA1', Blob.valueOf(canonical), Blob.valueOf(secretAccessKey));
       
       
        String signature = EncodingUtil.base64Encode(bsig);
        return signature;
    }


Monday, 24 December 2012

Apex Generation Failed Unable to find element + Amzon S3

Hi,

While generating Apex code from wsdl provided by Amazon (For S3) I got the above mentioned error. Below is a snippet from WSDL file.

 <xsd:schema
     elementFormDefault="qualified"
     targetNamespace="http://s3.amazonaws.com/doc/2006-03-01/">
      <xsd:include schemaLocation="AmazonS3.xsd"/>
    </xsd:schema>


To Resolve the error I downloaded XSD file from Amzon only and replaced the content of tag <xsd:include> with the content of the XSD file, and I was able to generate the Apex code file.