check if object exists in Cloud Files (PHP API)
Asked Answered
S

5

6

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

Shaitan answered 10/8, 2011 at 18:37 Comment(3)
If it throws an error, try catching it.Lietman
@Marc B-- that does work-- I was just looking for info on whether there's a "file exists" type of function I could use rather than try/catchShaitan
There's not one that I'm aware of, but if there were, it would probably just be a wrapper that does a try/catch anyway. So that's probably still your best bet ;)Telethon
E
4

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.";
}
Eyelash answered 22/8, 2011 at 2:24 Comment(0)
C
1

I have more general way to check if object exists:

    try {
        $this->_container->get_object($path);
        $booExists = true;
    } catch (Exception $e) {
        $booExists = false;
    }
Cristophercristy answered 17/12, 2012 at 17:17 Comment(0)
I
0

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
Isolation answered 4/12, 2012 at 17:59 Comment(0)
C
0

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);
Cupro answered 7/7, 2016 at 15:10 Comment(0)
H
0

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';
}
Harriot answered 20/9, 2016 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.