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. 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
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
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
. So get_objects(0, NULL, NULL, 'css')
, etc. Still seems like there's a better way though.
If you are using the old php-cloudfiles bindings, use the list_objects() method. 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