phpguzzlerackspace-cloudrackspacerackspace-cloudfiles

PHP - Rackspace CloudFiles API check if the file path already exists?


I'm working with the Rackspace CloudFiles API, where I've got a requirement to check for folder exists inside a container.

I have searched across different areas I could find solution to find if container exists or the object exists.

For example:

Format: container/folder/subfolder/file

Actual file stored location: test/test_folder/test_subfolder/test.txt

I want to know is my container has already got a folder called test_folder

And I came across something which says path, but am not sure we have this method exists!

getObjects ->> 'path', 'file path to search'

Solution

  • Even though @hurricanerix answer helped me, I want to post exact PHP method and snippet to help other is future.

    $client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
        'username' => 'XXXXXXXX',
        'apiKey'   => 'XXXXXXXXXXXXXXXXXXX'
    ));
    
    $total              = 0;
    $containerObjects   = array();
    $objectStoreService = $client->objectStoreService(null, 'XX');
    $container          = $objectStoreService->getContainer('test');
    $objData = $container->ObjectList(array('prefix'=>"test_folder/", 'delimiter'=>"/"));
    if(is_object($objData))
    {
        $total = $objData->count();
        if ($total == 0)
            break;
    
        foreach ($objData as $object) {
            $containerObjects[] = $object->getName();
        }
    }
    
    var_dump($containerObjects);
    

    Output

    array (size=1)
      0 => string 'test/test_folder/test_subfolder/' (length=32)
    

    Hope this will help.