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
streetstreet 

HTTP RESPONSE ERROR

Getting Below Error when trying to delete file from AMAZON

 

23:47:09.133 (133643000)|SYSTEM_METHOD_ENTRY|[306]|System.Http.send(APEX_OBJECT)
23:47:09.133 (133797000)|CALLOUT_REQUEST|[306]|System.HttpRequest[Endpoint=https://s3.amazonaws.com/bucketname/filename, Method=DELETE]
23:47:09.250 (250054000)|CALLOUT_RESPONSE|[306]|System.HttpResponse[Status=Bad Request, StatusCode=400]

 

HERE UNDER BUCKET NAME AND FILENAME Im Providing Respective bucket name and file name which has to be deleted.

 

 

 

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

 

String stringToSign = 'DELETE\n' +
'\n' +
'\n' +
dateString + '\n' +
('https://s3.amazonaws.com/bucketname goes here/file name goes here').replaceAll(' ', '');

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

System.debug('FINDME::stringToSign - ' + stringToSign);

Blob mac = Crypto.generateMac('hmacSHA1',Blob.valueOf(stringToSign), Blob.valueOf('secret'));
stringToSign = EncodingUtil.base64Encode(mac);

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

HttpRequest con = new HttpRequest();

con.setHeader('Authorization','AKIAIO55ACM6IO2EACFQ:' + stringToSign);
con.setEndPoint('https://s3.amazonaws.com'+'/bucket name goes here/file name here');


con.setHeader('Host','BN.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;
string ABD ='ABD';
system.debug('*&*&*&*&*'+ABD);
}

return null;
}