How to check on Rackspace if the file is uploaded or not?

677 views Asked by At

I'm working with the Rackspace PHP API, where I've got a requirement to check the file, if it exists then do something and if not then do something.

try {
  $file = $container->getObject($end_element);
  $fileExists = TRUE;
}
catch(Exception $e) {
  $fileExists = FALSE;
}
if ($fileExists) {
  // File is their, it needs to be rewrite/overwrite
  $file->setContent(fopen('sites/default/files/rackspace/' . $end_element, 'r+'));
  $file->update();
  // I'm getting this http://docs.rackspace.com/sdks/api/php/class-OpenCloud.ObjectStore.Resource.DataObject.html which I printted print_r($file->update());
}
else {
  // New file just to upload
  $container->uploadObject($end_element, fopen('sites/default/files/rackspace/' . $end_element, 'r+'), array());
}
1

There are 1 answers

1
hohner On BEST ANSWER

To see whether an object exists in a remote container, try using the objectExists method like so:

if ($container->objectExists('objectName.txt')) {
   // The object exists
} else {
   // The object doesn't exist
}

This will perform a HEAD request on that object, wrapping any 404 failure response in a try/catch block for you.


In terms of finding out the date the object was created, the API only tells you the date of last modification. This will be the create date if you haven't modified the object since it was first uploaded.

To find out the last modified datetime, you need to run:

$object = $container->getObject('objectName.txt');
$created = $object->getLastModified();