Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

Amazon AWS IOS SDK: How to list ALL file names in a FOLDER

I'm using AWS IOS SDK and trying to list all the files in a FOLDER.

This code works to list all files etc in a BUCKET:

-(void) s3DirectoryListing: (NSString *) bucketName {

    s3Client = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY withSecretKey:SECRET_KEY];

    @try {

        S3ListObjectsRequest *req = [[S3ListObjectsRequest alloc] initWithName:bucketName];

        S3ListObjectsResponse *resp = [s3Client listObjects:req];

        NSMutableArray* objectSummaries = resp.listObjectsResult.objectSummaries;  

        for (int x = 0; x < [objectSummaries count]; x++) {
            NSLog(@"objectSummaries: %@",[objectSummaries objectAtIndex:x]);
        }
    }
    @catch (NSException *exception) {
    NSLog(@"Cannot list S3 %@",exception);
}

}

So if I pass a bucketName name, this works. However if I try and pass bucketName/folderName" I get an error message. This code doesn't seem to like the combination of bucket and folder path combinations.

Any ideas appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Amazon S3 is a "flat" file system, meaning it does not have physical folders. "Folders" as you know are simply prefixes added to file names.

You need to set the prefix property to your request (see the documentation)

For example, given the following set of files:

folderName1/fileNameA.txt
folderName1/fileNameB.txt
folderName2/folderName3/fileNameC.txt

If you set prefix with folderName1, and your delimiter with /, you should get only the first two entries.

Last, but not least, leave your bucketName only with the bucket name :)

More info at the S3 Developer Guide.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...