public bool DeleteFileFromS3(AmazonDeleteRequest payload) { var accessKey = ConfigurationManager.AppSettings["AWSAccessKey"]; // Get access key from a secure store var secretKey = ConfigurationManager.AppSettings["AWSSecretKey"]; // Get secret key from a secure store string filePath = payload.FileUrl; string message = string.Empty; bool retVal = false; using (AmazonS3Client client = new AmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.USWest2)) { try { DeleteObjectRequest req = new DeleteObjectRequest() { BucketName = bucketName, Key = filePath }; DeleteObjectResponse resp = client.DeleteObject(req); if (resp.HttpStatusCode == HttpStatusCode.NoContent) { // success code retVal = true; } else { retVal = false;// unsuccessful } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { message = "Check the provided AWS Credentials. \n For service sign up go to http://aws.amazon.com/s3"; //give back the error to main() throw new Exception("Check the provided AWS Credentials."); } else { message = String.Format("Error occurred. Message:'{0}' when writing an object", amazonS3Exception.Message); //throw new Exception("Error occurred: " + amazonS3Exception.Message); } } } return(retVal); }
//DELETE file from Amazon server public bool DeleteFile(int id) { bool retVal = false; UserFile fileInfo = FileMetaService.FileGetById(id); AmazonDeleteRequest payload = new AmazonDeleteRequest(); payload.Id = fileInfo.ID; payload.FileUrl = fileInfo.FileUrl; retVal = _uploadService.DeleteFileFromS3(payload); if (retVal) { fms.FileDelete(payload.Id); } ; return(retVal); }