Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if object exists in Cloud Files (PHP API)

I've just started working with the PHP API for Rackspace Cloud Files. So far so good-- but I am using it as sort of a poor man's memcache, storing key/value pairs of serialized data.

My app attempts to grab the existing cached object by its key ('name' in the API language) using something like this:

$obj = $this->container->get_object($key);

The problem is, if the object doesn't exist, the API throws a fatal error rather than simply returning false. The "right" way to do this by the API would probably be to do a

$objs = $this->container->list_objects();

and then check for my $key value in that list. However, this seems way more time/CPU intensive than just returning false from the get_object request.

Is there a way to do a "search for object" or "check if object exists" in Cloud Files?

Thanks

like image 982
julio Avatar asked Aug 10 '11 18:08

julio


1 Answers

I sent them a pull request and hope it'll get included.

https://github.com/rackspace/php-cloudfiles/pull/35

My pull-request includes an example, for you it would be similar to this:

$object = new CF_Object($this->container, 'key');
if ($object->exists() === false) {
    echo "The object '{$object->name}' does not exist.";
}
like image 141
Till Avatar answered Sep 30 '22 07:09

Till