Check if an object exists in cloud files (PHP API)

I just started working with the PHP API for Rackspace cloud files. So far, so good, but I use it as a kind of poor person memcache, keeping the key / value pairs of serialized data.

My application is trying to grab an existing cached object by its key ("name" in the API language) using something like this:

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

The problem is that if the object does not exist, the API generates a fatal error, and not just returns false . The โ€œrightโ€ way to do this using the API would probably be to do

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

and then check my $key value in this list. However, this seems more intense and intense than just returning false from the get_object request.

Is there a way to "search for an object" or "check if an object exists" in cloud files?

thanks

+6
source share
5 answers

I sent them a pull request and hope it will be turned on.

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

My pull request includes an example, it will look like this for you:

 $object = new CF_Object($this->container, 'key'); if ($object->exists() === false) { echo "The object '{$object->name}' does not exist."; } 
+4
source

I have a more general way to check if an object exists:

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

If you reset the $ object, you will see that content_length is zero. Or the last changed will be a string of zero length.

Example:

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

In addition, there is 404 in the unloaded parent object, which will be returned, but it will be closed, so you will need to crack it a bit.

To see this, do the following:

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

You will see inside this response_status object, which is 404

 [response_status:CF_Http:private] => 404 
0
source

I know I'm a little late to the party, but hopefully this helps someone in the future: you can use the objectExists() method to check the availability of the object.

 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 as ...

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

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'; } 
0
source

Source: https://habr.com/ru/post/894748/


All Articles