Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 428
Tim Arnold Avatar asked Aug 13 '12 19:08

Tim Arnold


People also ask

How do I delete an object in AWS?

To delete the object, select the object, and choose delete and confirm your choice by typing delete in the text field. On, Amazon S3 will permanently delete the object version. Select the object version that you want to delete, and choose delete and confirm your choice by typing permanently delete in the text field.

How do I delete an object in AWS CLI?

You can delete objects by explicitly calling DELETE Object or configure its lifecycle (PutBucketLifecycle ) to enable Amazon S3 to remove them for you.

How do I remove an object from a bucket?

Deleting an objectIn the Buckets list, choose the name of the bucket that you want to delete an object from. Select the check box to the left of the names of the objects that you want to delete. Choose Actions and choose Delete from the list of options that appears.

How do you delete an object from a glacier?

You cannot delete an archive using the Amazon S3 Glacier (S3 Glacier) management console. To delete an archive you must use the AWS Command Line Interface (CLI) or write code to make a delete request using either the REST API directly or the AWS SDK for Java and . NET wrapper libraries.


3 Answers

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

like image 134
Lukas Roberts Avatar answered Nov 02 '22 22:11

Lukas Roberts


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

[s3Client deleteObjectWithKey:@"objectKey" withBucket:@"my-bucket"];
like image 34
Art Gillespie Avatar answered Nov 02 '22 23:11

Art Gillespie


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];
like image 28
Myxtic Avatar answered Nov 02 '22 23:11

Myxtic