Amazon AWS IOS SDK:如何列出文件夹中的所有文件名
我正在使用 AWS IOS SDK 并尝试列出文件夹中的所有文件.
I'm using AWS IOS SDK and trying to list all the files in a FOLDER.
此代码用于列出 BUCKET 中的所有文件等:
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);
}
}
因此,如果我传递一个 bucketName 名称,这将起作用.但是,如果我尝试通过 bucketName/folderName" 我收到一条错误消息.此代码似乎不喜欢存储桶和文件夹路径组合的组合.
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.
任何想法表示赞赏.
Amazon S3 是一个平面"文件系统,这意味着它没有物理文件夹.正如您所知,文件夹"只是添加到文件名的前缀.
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.
您需要为您的请求设置 prefix
属性(请参阅 文档)
You need to set the prefix
property to your request (see the documentation)
例如,给定以下文件集:
For example, given the following set of files:
文件夹名1/文件名A.txt
文件夹名称1/文件名称B.txt
folderName2/folderName3/fileNameC.txt
folderName1/fileNameA.txt
folderName1/fileNameB.txt
folderName2/folderName3/fileNameC.txt
如果你用 folderName1
设置 prefix
,用 /
设置你的 delimiter
,你应该只得到第一个两个条目.
If you set prefix
with folderName1
, and your delimiter
with /
, you should get only the first two entries.
最后但并非最不重要的一点是,将您的 bucketName
仅保留存储桶名称:)
Last, but not least, leave your bucketName
only with the bucket name :)
在 S3 开发人员指南中了解更多信息.