6

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

user5739562
  • 190
  • 1
  • 8

2 Answers2

17

Your code to delete a file from S3 bucket should look something like this:

    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

Rohan Dubal
  • 847
  • 5
  • 10
  • Hi Rohan, Please suggest me how will I use this method or kind of like above to delete object from bucket using TransferUtility. By using steps suggested by you when I call S3.deleteObject function there is exception I am getting i.e. "The service configuration is `nil`. You need to configure `awsconfiguration.json`, `Info.plist` or set `defaultServiceConfiguration` before using this method." . I had already included `awsconfiguration.json file in my project directory. Please help me on this as soon as possible. I am stuck. – Vivek Gupta Apr 16 '18 at 07:40
  • To solve that you can copy the S3TransferUtility section from awsconfiguration.json and paste it with the key of "S3" so that S3 configuration can be loaded as well. – Rohan Dubal May 07 '18 at 22:02
  • could a malicious user delete all objects buy putting a path of asterisk dot asterisk or something like that? to do the above you would have to allow deleteObject on your security settings for your s3 access – alionthego Oct 26 '18 at 14:51
  • delete object has to be called on an individual file. so if the user does not have listObjects permission they cannot get all files and would not be able to delete all files – Rohan Dubal Oct 26 '18 at 19:56
  • hi this method gives me success but file is not deleted from bucket. what can be the reason? – Krutika Sonawala Oct 11 '21 at 13:02
2

Swift 4+

If your not initial yet

let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.APSoutheast1, identityPoolId: "yourPoolID")

let configuration = AWSServiceConfiguration(region:.APSoutheast1, credentialsProvider:credentialsProvider)

AWSServiceManager.default().defaultServiceConfiguration = configuration

And

let s3 = AWSS3.default()
guard let deleteObjectRequest = AWSS3DeleteObjectRequest() else {
    return
}
deleteObjectRequest.bucket = "yourBucketName"
deleteObjectRequest.key = "yourFileName"
s3.deleteObject(deleteObjectRequest).continueWith { (task:AWSTask) -> AnyObject? in
    
    if let error = task.error {
        print("Error occurred: \(error)")
        return nil
    }
    print("Deleted successfully.")
    return nil
    
}