Hi Guys
Rackspace cloud files is an awesome file storage system, We upload a file and can make it publicly available through their CDN.
While I use Cloud files in pretty much all my projects at work or personal stuff, one thing that I did today was to delete objects with specific prefix
e.g.
Lets say I have these objects under container “jcorg”
- hello
- hello1
- hello2
- anothervaluableobject
and I want to delete all objects whose name starts with hello
Here is a code snippet that will be handy for you
Language: PHP
API link: https://github.com/rackspace/php-opencloud
and of course you can use equivalent code in other language SDKs too
Code Snippet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$connection = new \OpenCloud\Rackspace(AUTHURL_US_OR_UK, array('username' => USERNAME, 'apiKey' => APIKEY)); // now, connect to the ObjectStore service $objstore = $connection->objectStoreService('cloudFiles', 'SYD'); $container = $objstore->getContainer("jcorg"); if (is_object($container)) { $options = array( 'prefix' => 'hello' ); $objects = $container->objectList($options); foreach ($objects as $object) { /** @var $object OpenCloud\ObjectStore\Resource\DataObject **/ $object->delete(); } } |
Let me give you a brief overview to you
- First we are creating a connection to Rackspace server, depending on which region you are in you will use either RACKSPACE_US or RACKSPACE_UK to authenticate your request.
123$connection = new \OpenCloud\Rackspace(AUTHURL_US_OR_UK,array('username' => USERNAME,'apiKey' => APIKEY)); - Then we are connection to our object store
123// now, connect to the ObjectStore service$objstore = $connection->objectStoreService('cloudFiles', 'SYD');$container = $objstore->getContainer("jcorg"); - Then we are getting hold of object list where name or an object starts with “hello”
1234$options = array('prefix' => 'hello');$objects = $container->objectList($options); - Finally we are iterating on objects found and deleting them. You can check object name here to just to filter them out before you process them for deletion
1234foreach ($objects as $object) {/** @var $object OpenCloud\ObjectStore\Resource\DataObject **/$object->delete();}
I hope this makes sense, but just in case if you have a question, do leave your comment.
Leave a Reply