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?
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.
You can delete objects by explicitly calling DELETE Object or configure its lifecycle (PutBucketLifecycle ) to enable Amazon S3 to remove them for you.
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.
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.
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
Just going off the documentation you linked to, but does this not work?
[s3Client deleteObjectWithKey:@"objectKey" withBucket:@"my-bucket"];
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With