簡體   English   中英

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

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

使用Rackspace CloudFiles API(在PHP中),有時我只需要獲取容器中所有當前文件的列表。 我剛剛提出的是非常慢和無效,因為它獲得了與該文件相關的每個對象。 所以我有:

我的功能

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

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 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-cloudfiles綁定,請使用list_objects()方法。 這將只返回容器中的對象列表。

php-cloudfiles綁定現已棄用,新的​​官方php cloudfiles綁定是php-opencloud(對象存儲) ,你可以在這里找到關於列出容器的部分

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

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

$obj具有與列表返回的對象相關聯的所有元數據(也就是說,某些屬性只能通過直接調用對象來檢索,但這應該具有您需要的大部分內容)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM