How to use AWS iOS SDK to delete an object?

Loading...

How to use AWS iOS SDK to delete an object?

I'm trying to delete a public image (cannedACL property of the S3PutObjectRequest to upload the image was [S3CannedACL publicRead]) uploaded to an S3 bucket.
There is an S3DeleteObjectRequest class in the AWS SDK documentation, but there don't seem to be any properties or initialization methods for this class.
Based on sample code in other languages, it looks like there should be key or bucket property or an initialization method setting those properties, as there are in the iOS SDK's S3PutObjectRequest class, used to upload files to a bucket.
What's going on here? Is the SDK simply incomplete? Is there a way to delete an object with the iOS AWS SDK?

Solutions/Answers:

Answer 1:

Just going off the documentation you linked to, but does this not work?

[s3Client deleteObjectWithKey:@"objectKey" withBucket:@"my-bucket"];

Answer 2:

For the iOS SDK for S3 V2, this code came in handy

AWSS3 *s3 = [AWSS3 defaultS3];
                AWSS3DeleteObjectRequest *deleteRequest = [AWSS3DeleteObjectRequest new];
                deleteRequest.bucket = S3BucketName;
                deleteRequest.key = climb.imageKey;
                [[[s3 deleteObject:deleteRequest] continueWithBlock:^id(BFTask *task) {
                    if(task.error != nil){
                        if(task.error.code != AWSS3TransferManagerErrorCancelled && task.error.code != AWSS3TransferManagerErrorPaused){
                            NSLog(@"%s Error: [%@]",__PRETTY_FUNCTION__, task.error);
                        }
                    }else{
                        // Completed logic here
                    }
                    return nil;
                }] waitUntilFinished];

This is based heavily upon the unit tests that have been written for the library here: https://github.com/aws/aws-sdk-ios/blob/master/AWSS3Tests/AWSS3Tests.m

Answer 3:

Art Gillespie’s answer worked just fine for me as well.

However, I also discovered that you can achieve the same by setting the key and bucket attributes on the S3 delete object request:

S3DeleteObjectRequest *dor = [[S3DeleteObjectRequest alloc] init];
dor.key = AWS_OBJ_PATH;
dor.bucket = AWS_BUCKET;

[s3Client deleteObject:dor];

References

Loading...