简体   繁体   English

rackspace cloudfiles api - 返回容器文件的最有效方法

[英]rackspace cloudfiles api - most efficient method to return container files

Using the Rackspace CloudFiles API (in PHP), there are times when I need to get just a list of the all the current files in the container. 使用Rackspace CloudFiles API(在PHP中),有时我只需要获取容器中所有当前文件的列表。 What I just came up with is terribly slow and in-efficient because it gets every object pertaining to that file. 我刚刚提出的是非常慢和无效,因为它获得了与该文件相关的每个对象。 So what I have: 所以我有:

My Function 我的功能

function clean_cdn() {
    $objects = $this->CI->cfiles->get_objects();
    foreach ($objects as $object) {
        echo $object->name;
    }
}

get_objects wrapper for CodeIgniter getIobjects CodeIgniter的包装器

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

This will give me just the name (which is all I need for what I'm doing currently) but is there a better way? 这将给我一个名字(这就是我目前所做的一切)但有更好的方法吗?

Update 更新

I noticed that I could technically just put all the "directories" in an array and iterate over them in a foreach loop, listing each of them as the 4th parameter of get_objects . 我注意到我可以在技术上将所有“目录”放在一个数组中并在foreach循环中迭代它们,将它们列为get_objects的第四个参数。 So get_objects(0, NULL, NULL, 'css') , etc. Still seems like there's a better way though. 所以get_objects(0, NULL, NULL, 'css')等等。看起来还是有更好的方法。

If you are using the old php-cloudfiles bindings, use the list_objects() method. 如果您使用旧的php-cloudfiles绑定,请使用list_objects()方法。 This will just return a list of the objects in the container. 这将只返回容器中的对象列表。

php-cloudfiles bindings are deprecated now, the new official php cloudfiles bindings are php-opencloud (object-store) and you can find the section on listing objects in a container here php-cloudfiles绑定现已弃用,新的​​官方php cloudfiles绑定是php-opencloud(对象存储) ,你可以在这里找到关于列出容器的部分

Using php-opencloud, if you have a Container object, use the ObjectList() method to return a list of objects: 使用php-opencloud,如果你有一个Container对象,使用ObjectList()方法返回一个对象列表:

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

The $obj has all of the metadata associated with the object that's returned by the list (which is to say, there are certain attributes that can only be retrieved by invoking the object directly, but this should have most of what you need). $obj具有与列表返回的对象相关联的所有元数据(也就是说,某些属性只能通过直接调用对象来检索,但这应该具有您需要的大部分内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM