Q:

How to list the folders at a specific path from an Amazon S3 bucket, in Objective C?

After I've read the Amazon documentation and I didn't seem to find how to list the folders name at a specific path from my Amazon S3 bucket, I also looked around on StackOverflow with no luck I answered my own question.
I will post it here and hope that it will also help others :)

A:

And here is my code:
// pass path as: @"backup/"
- (void)listAmazonFoldersAtPath:(NSString *)path
                     completion:(void (^)(BOOL success, NSMutableArray *filesArray))completionBlock
{

    NSMutableArray *collection = [[NSMutableArray alloc] init];

    AWSS3 *s3 = [AWSS3 defaultS3];

    AWSS3ListObjectsRequest *listObjectsRequest = [AWSS3ListObjectsRequest new];
    listObjectsRequest.prefix = path;
    listObjectsRequest.delimiter = @"/";

    listObjectsRequest.bucket = kAmazonS3Bucket;
    [[s3 listObjects:listObjectsRequest] continueWithBlock:^id(AWSTask *task) {
        if (task.error) {
            NSLog(@"listObjects failed: [%@]", task.error);
            completionBlock(NO, collection);
        } else {
            AWSS3ListObjectsOutput *listObjectsOutput = task.result;
            for (AWSS3CommonPrefix *s3ObjectPrefix in listObjectsOutput.commonPrefixes) {
                if(s3ObjectPrefix.prefix && s3ObjectPrefix.prefix.length > 0){
                    NSString *fileName = [[s3ObjectPrefix.prefix componentsSeparatedByString:@"/"] lastObject];
                    if (fileName && fileName.length > 0) {
                        [collection addObject:s3ObjectPrefix.prefix];
                    }
                }
            }

            completionBlock(YES, collection);
            NSLog(@"Current folders: %@", collection);

        }
        return nil;
    }];

}

I hope it will help others.
NOTE: If you also want to list the subfolder please feel free to comment the line:
listObjectsRequest.delimiter = @"/";

