使用 iOS SDK 获取具有特定扩展名的文件

Fetch files with specific extensions using iOS SDK

我想获取文件夹项目 API 中的特定文件。现在我正在获取所有文件,我必须自己过滤它。

https://box-content.readme.io/reference#get-a-folders-items

搜索APIhttps://box-content.readme.io/reference#searching-for-content 如果我想搜索具有特定扩展名的所有文件。我尝试将查询搜索为空字符串并返回错误

Error Domain=com.box.contentsdk.errordomain Code=400 "The operation couldn’t be completed. (com.box.contentsdk.errordomain error 400.)" UserInfo=0x7ff34a59f7b0 {com.box.contentsdk.jsonerrorresponse=<CFBasicHash 0x7ff34c46b270 [0x10ed8b180]>{type = immutable dict, count = 7,
entries =>
0 : <CFString 0x7ff34c4b3ef0 [0x10ed8b180]>{contents = "status"} = <CFNumber 0xb000000000001903 [0x10ed8b180]>{value = +400, type = kCFNumberSInt64Type}
1 : <CFString 0x7ff34c4c1550 [0x10ed8b180]>{contents = "help_url"} = <CFString 0x7ff34c4760d0 [0x10ed8b180]>{contents = "http://developers.box.com/docs/#errors"}
4 : <CFString 0x7ff34c47d2e0 [0x10ed8b180]>{contents = "code"} = <CFString 0x7ff34c4cbaf0 [0x10ed8b180]>{contents = "bad_request"}
5 : <CFString 0x7ff34c48d6a0 [0x10ed8b180]>{contents = "request_id"} = <CFString 0x7ff34c445920 [0x10ed8b180]>{contents = "130336355b6746254f8f"}
6 : <CFString 0x7ff34c4cb960 [0x10ed8b180]>{contents = "context_info"} = <CFBasicHash 0x7ff34c4bc5b0 [0x10ed8b180]>{type = immutable dict, count = 1,
entries =>
1 : <CFString 0x7ff34c43f560 [0x10ed8b180]>{contents = "errors"} = <CFArray 0x7ff34c4bb1e0 [0x10ed8b180]>{type = immutable, count = 1, values = (
0 : <CFBasicHash 0x7ff34c46a510 [0x10ed8b180]>{type = immutable dict, count = 3,
entries =>
0 : <CFString 0x7ff34c476ff0 [0x10ed8b180]>{contents = "name"} = <CFString 0x7ff34c4bc380 [0x10ed8b180]>{contents = "to_search"}
1 : <CFString 0x7ff34c4bc3a0 [0x10ed8b180]>{contents = "message"} = <CFString 0x7ff34c4bcc60 [0x10ed8b180]>{contents = "Invalid value ''."}
2 : <CFString 0x7ff34c476fd0 [0x10ed8b180]>{contents = "reason"} = <CFString 0x7ff34c4b8500 [0x10ed8b180]>{contents = "invalid_parameter"}
}

)}
}

7 : <CFString 0x7ff34c4bc3a0 [0x10ed8b180]>{contents = "message"} = <CFString 0x7ff34c476100 [0x10ed8b180]>{contents = "Bad Request"}
9 : <CFString 0x7ff34a52b970 [0x10ed8b180]>{contents = "type"} = <CFString 0x7ff34a595d80 [0x10ed8b180]>{contents = "error"}
}
}

请告诉我如何获取特定类型的所有文件。

如何使用扩展本身作为查询,例如

curl -X GET https://api.box.com/2.0/search?query=.jpg -H "Authorization: Bearer "

然后您可以使用其他查询参数选项(例如祖先文件夹 ID)对其进行限制。

我在 BOX-APIs.

上发表一些我的观察

首先,对于 BOX 服务,无法一次获取所有 files_list,因为很少有用户可以拥有数百万个文件。因此,一次获取属于少数特定类型 (如仅 .jpg/.png) 的文件将花费时间,因为我认为服务器端实现必须经过用户搜索该给定类型的所有文件列表。

但是有一种方法可以使用搜索 API 来获取具有某些文件扩展名的文件。

//Sample example

BOXContentClient *contentClient = [BOXContentClient defaultClient];
NSString *searchQuery = <query - text>;
NSArray *fileExtensions = <array of extensions required>;
BOXSearchRequest *searchRequest = [contentClient searchRequestWithQuery:searchQuery 
                                                                inRange:NSMakeRange(0, 9999)]; // max of 10000 files for
searchRequest.requestAllItemFields = YES; // will fetch all the fields for the files
searchRequest.fileExtensions = fileExtensions; // set the extensions for the files needed
searchRequest.type = @"file"; // searching for files , can be extended for folders
searchRequest.contentTypes = @[@"name"]; // Search in Names only , it can search in file content also incase of text related files (.txt, .doc)
[searchRequest performRequestWithCompletion:^(NSArray *items, NSUInteger totalCount, NSRange range, NSError *error) {
 // Do what ever you want
}];

@JohnHoerr 推荐的另一个好答案Get all files in box account

希望这对正在寻找它的人有所帮助 :)