使用 Rackspace CloudFiles API(在 PHP 中),有时我只需要获取容器中所有当前文件的列表。我刚想出的方法非常缓慢且效率低下,因为它获取了与该文件有关的每个对象。所以我有什么:
我的功能
function clean_cdn() {
$objects = $this->CI->cfiles->get_objects();
foreach ($objects as $object) {
echo $object->name;
}
}
CodeIgniter 的 get_objects 包装器
public function get_objects() {
$my_container = $this->container_info();
try {
return $my_container->get_objects(0, NULL, NULL, NULL);
} catch(Exception $e) {
$this->_handle_error($e);
return FALSE;
}
}
云文件 get_objects 函数
function get_objects($limit=0, $marker=NULL, $prefix=NULL, $path=NULL)
{
list($status, $reason, $obj_array) =
$this->cfs_http->get_objects($this->name, $limit,
$marker, $prefix, $path);
if ($status < 200 || $status > 299) {
throw new InvalidResponseException(
"Invalid response (".$status."): ".$this->cfs_http->get_error());
}
$objects = array();
foreach ($obj_array as $obj) {
$tmp = new CF_Object($this, $obj["name"], False, True);
$tmp->content_type = $obj["content_type"];
$tmp->content_length = (float) $obj["bytes"];
$tmp->set_etag($obj["hash"]);
$tmp->last_modified = $obj["last_modified"];
$objects[] = $tmp;
}
return $objects;
}
这会给我一个名字(这是我目前正在做的事情所需要的)但是有更好的方法吗?
更新
我注意到从技术上讲,我可以将所有“目录”放在一个数组中,然后在 foreach 循环中迭代它们,将它们中的每一个列为get_objects
. 所以get_objects(0, NULL, NULL, 'css')
,等等。不过,似乎还有更好的方法。