• Charlie9
  • NEWBIE
  • 0 Points
  • Member since 2012

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

Hello,

 

I was hoping someone could post an example of a amazon s3 REST delete method that has worked. Does versioning need to be enabled for REST delete to work?

 

OR possibly spot whats wrong with this?

 

 

public pageReference deleteMode(){
    queryResults = [select ID,
                           file_name__c,
                           file_ID__c,
                           matter_number__c,
                           client_number__c,
                           fileURL__c
                    from input_form__c
                    where ID = :inputFormID];
                    
    input_form__c ifc = queryResults[0];

    fileURL = ifc.fileURL__c;

    String dateString = Datetime.now().formatGmt('EEE, dd MMM yyyy HH:mm:ss Z');

    stringToSign = 'DELETE\n' +
                   '\n' +
                   '\n' +
                   dateString + '\n' +
                   ('/xxxx' + fileURL).replaceAll(' ', '');

    //stringToSign = stringToSign.replaceAll(' ', '%20');

    System.debug('FINDME::stringToSign - ' + stringToSign);
    Blob mac = Crypto.generateMac('hmacSHA1',  Blob.valueOf(stringToSign), Blob.valueOf('xxxx'));
    stringToSign = EncodingUtil.base64Encode(mac);

    //String encoded = EncodingUtil.urlEncode(stringToSign, 'UTF-8'); 

    HttpRequest con = new HttpRequest();
    con.setHeader('Authorization','AWS xxxx:' + stringToSign);
    con.setEndPoint('http://xxxx.s3.amazonaws.com' + fileURL);
    con.setHeader('Host','xxxx.s3.amazonaws.com');

    con.setHeader('Date', dateString);
    con.setMethod('DELETE');
    
    Http http = new Http();
    HTTPResponse res = http.send(con);

    System.debug('RES.GETBODY: ' + res.getBody() + ' RES.GETSTATUS: ' + res.getStatus() + ' CON.GETENDPT: ' + con.getEndPoint());

    if (res.getStatusCode() >= 400) {
delete ifc;
}       

return null;
    }



res.getBody() is empty and res.getStatus is 'no content' which I think makes sense since its delete. But the file never gets deleted from s3.

 

Thanks!

 

Max