【问题标题】:rackspace cloudfiles api - most efficient method to return container filesrackspace cloudfiles api - 返回容器文件的最有效方法
【发布时间】:2013-05-29 14:52:36
【问题描述】:

使用 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;
    }
}

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

【问题讨论】:

    标签: php rackspace-cloud cloudfiles


    【解决方案1】:

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

    php-cloudfiles 绑定现在已弃用,新的​​官方 php cloudfiles 绑定为php-opencloud (object-store),您可以找到关于在容器中列出对象的部分here

    【讨论】:

    • 很好,谢谢。我会检查这个。猜猜是时候升级了。
    【解决方案2】:

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

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

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

    【讨论】:

    • 我使用的是旧的 php 绑定,但这显然适用于 opencloud。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 2011-02-15
    相关资源
    最近更新 更多