1

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'
GoSmash
  • 1,096
  • 1
  • 11
  • 38

2 Answers2

1

I can't really answer this question in the context of PHP or Guzzle, but I can provide some info on Cloud Files and some command line curl examples that might help.

Rackspace Cloud Files does not really have the concept of "folders" inside containers. You have a URL with the following pieces: https://{storage_endpoint}/{account_name}/{container_name}/{object_name}

You can however emulate folders by creating objects with the "/" character in the name. Then when performing a container listing, you can set the "prefix" and "delimiter" query string parameters to emulate a folder-like behavior.

For example, say you do a listing of your container to see all your objects:

$ curl -i -XGET -H"x-auth-token: $AUTH_TOKEN" https://storage101.iad3.clouddrive.com/v1/$ACCOUNT_NAME/$CONTAINER_NAME                       
HTTP/1.1 200 OK
...
Date: Thu, 01 Dec 2016 02:16:53 GMT

foo/file1.txt
foo/bar/file2.txt
baz/file3.txt
baz/file4.txt

Here we have four objects named "foo/file1.txt", "foo/bar/file2.txt", "baz/file3.txt" and "baz/file4.txt".

The easiest way to test for the existence of the folder "foo" would be to set the prefix to "foo/" and the delimiter to "/" as follows:

$ curl -i -XGET -H"x-auth-token: $AUTH_TOKEN" https://storage101.iad3.clouddrive.com/v1/$ACCOUNT_NAME/$CONTAINER_NAME?prefix=foo/&delimiter=/
HTTP/1.1 200 OK
...
Date: Thu, 01 Dec 2016 02:16:45 GMT

foo/file1.txt
foo/bar/file2.txt

As you can see, if any objects exist with that prefix, then a HTTP 200 is returned along with a listing of the objects that matched the prefix. If no objects exist with that prefix, the "folder" does not exist and a HTTP 204 will be returned.

If you now want to test for a "sub-folder" inside "foo", you can just perform another container listing changing the prefix to "foo/bar/" and keeping the delimiter of "/".

Further, if you want to create a folder "images" and have it contain "cat.jpg", you don't need to create a folder first, you just create an object called "images/cat.jpg" that contains the data for cat.jpg.

You can find more information about Rackspace Cloud Files and Pseudo-Hierarchical folders here: https://developer.rackspace.com/docs/cloud-files/v1/general-api-info/pseudo-hierarchical-folders-and-directories/

hurricanerix
  • 101
  • 3
1

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.

GoSmash
  • 1,096
  • 1
  • 11
  • 38