Question

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

Was it helpful?

Solution

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

OTHER TIPS

I have more general way to check if object exists:

    try {
        $this->_container->get_object($path);
        $booExists = true;
    } catch (Exception $e) {
        $booExists = false;
    }

If you dump the $object, you'll see that content_length is zero. Or, last modified will be a zero length string.

Example:

$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->content_length);

There is also, deep in the dumped parent object, a 404 that will return, but it's private, so you'd need to some hackin' to get at it.

To see this, do the following:

$object = new CF_Object($container, 'thisdocaintthere.pdf');
print_r($object->container->cfs_http);

You'll see inside that object a response_status that is 404

[response_status:CF_Http:private] => 404

I know I'm a little late to the party, but hopefully this will help someone in the future: you can use the objectExists() method to test if an object is available.

public static function getObject($container, $filename, $expirationTime = false)
{
    if ($container->objectExists($filename)) {

        $object = $container->getPartialObject($filename);

        // return a private, temporary url
        if ($expirationTime) {
            return $object->getTemporaryUrl($expirationTime, 'GET');
        }

        // return a public url
        return $object->getPublicUrl();
    }

    // object does not exist
    return '';
}

Use like...

// public CDN file
$photo = self::getObject($container, 'myPublicfile.jpg');

// private file; temporary link expires after 60 seconds
$photo = self::getObject($container, 'myPrivatefile.jpg', 60);

If you do not want to import opencloud to perform this check you can use the following:

$url = 'YOUR CDN URL';
$code = FALSE;
$options['http'] = array(
    'method' => "HEAD",
    'ignore_errors' => 1,
    'max_redirects' => 0
);
$body = file_get_contents($url, NULL, stream_context_create($options));
sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);


if($code!='200') {
    echo 'failed';
} else {
    echo 'exists';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top