0

使用 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'),等等。不过,似乎还有更好的方法。

4

2 回答 2

1

如果您使用旧的 php-cloudfiles 绑定,请使用 list_objects() 方法。这将只返回容器中对象的列表。

现在不推荐使用 php-cloudfiles 绑定,新的官方 php cloudfiles 绑定是php-opencloud (object-store) ,您可以在此处找到关于在容器中列出对象的部分

于 2013-05-29T15:24:54.323 回答
1

使用 php-opencloud,如果你有一个 Container 对象,使用该ObjectList()方法返回一个对象列表:

   $list = $container->ObjectList();
   while ($obj = $list->Next()) {
      // do stuff with $obj
   }

具有与列表返回的对象关联的$obj所有元数据(也就是说,某些属性只能通过直接调用对象来检索,但这应该具有您需要的大部分内容)。

于 2013-05-30T18:42:45.030 回答