Question:
I am writing an iOS app in Swift, and am using Amazon S3 to store files. I was wondering if anyone knows how I can programmatically delete files in my S3 bucket on command (instead of setting a delete policy in the bucket lifecycle).
Thanks in advance
Answer:
Your code to delete a file from S3 bucket should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
let s3 = AWSS3.defaultS3() let deleteObjectRequest = AWSS3DeleteObjectRequest() deleteObjectRequest.bucket = "yourBucketName" deleteObjectRequest.key = "yourFileName" s3.deleteObject(deleteObjectRequest).continueWithBlock { (task:AWSTask) -> AnyObject? in if let error = task.error { print("Error occurred: \(error)") return nil } print("Deleted successfully.") return nil } |
Thanks,
Rohan