You need to sign in to do that
Don't have an account?
Prateek Kumar 51
Could anyone help me out to delete a object from Amazon S3 bucket?
Hi,
I was trying to upload a file to Amazon S3 bucket using the following source code.
public class AmazonFileUpload { public static String uploadFile(Blob body, String name, String contentType, String appUName) { String uploaded_url; try { if(body != null && String.isNotBlank(name) && String.isNotBlank(contentType)) { String attachmentBody = EncodingUtil.base64Encode(body); String formattedDateString = Datetime.now().formatGMT('EEE, dd MMM yyyy HH:mm:ss z'); String key,secret,bucketname,host,filename,folderName; String method = 'PUT'; key = Amazon_Key__c; //From Custom Settings secret =Amazon_Secret__c; //From Custom Settings bucketname = Amazon_Bucket_Name__c; //From Custom Settings host = Amazon_Host__c; //From Custom Settings filename = name; folderName = appUName; folderName= folderName.replaceAll( '\\s+', ''); HttpRequest req = new HttpRequest(); req.setMethod(method); req.setEndpoint('http://' + bucketname + '.' + host + '/' + folderName + '/' + filename); req.setHeader('Host', bucketname + '.' + host); req.setHeader('Content-Length', String.valueOf(attachmentBody.length())); req.setHeader('Content-Encoding', 'UTF-8'); req.setHeader('Content-type', contentType); req.setHeader('Connection', 'keep-alive'); req.setHeader('Date', formattedDateString); req.setHeader('ACL', 'public-read'); Blob blobBody = EncodingUtil.base64Decode(attachmentBody); req.setBodyAsBlob(blobBody); filename = filename.replaceAll(' ', ''); String stringToSign = 'PUT\n\n' + contentType + '\n' + formattedDateString + '\n' + '/' + bucketname + '/' +folderName + '/' + filename; String encodedStringToSign = EncodingUtil.urlEncode(stringToSign, 'UTF-8'); system.debug('stringToSign='+stringToSign); Blob mac = Crypto.generateMac('HMACSHA1', blob.valueof(stringToSign),blob.valueof(secret)); String signed = EncodingUtil.base64Encode(mac); String authHeader = 'AWS' + ' ' + key + ':' + signed; req.setHeader('Authorization',authHeader); String decoded = EncodingUtil.urlDecode(encodedStringToSign, 'UTF-8'); Http http = new Http(); HTTPResponse res = new HTTPResponse(); res = http.send(req); String response = res.getBody(); string url = req.getEndpoint(); if(res.getStatus() == 'OK' && res.getStatusCode() == 200) { uploaded_url = 'https://'+Label.SIS_Amazon_Host+'/'+Label.SIS_Amazon_Bucket_Name+'/'+folderName+'/'+filename; } else { return res.toString(); } } else { return 'Something wrong happenned while uploading the file, please contact your system administrator.'; } } catch(Exception e) { return 'ERROR: Uploading file to Amazon - '+e.getMessage()+' - '+e.getLineNumber(); } return uploaded_url; } }
The above source code is working fine for uploading the document.
But, I want to add one more method to this class, where I can delete an object from S3.
Thank you,
Jonathan