简体   繁体   中英

Renaming Rackspace Folder/Container

I have the PHP library from Rackspace. We keep all files in a Container called 'data'. Within that container is a hierarchical directory of files.

I am able to rename or move an object, no problem (wrapped in my own class):

    $this->container->move_object_to('uploads/files/file.txt', 'data', 'uploads/files2/filecopy.txt');

But I'm not able to do the same with a folder:

    $this->container->move_object_to('uploads/files', 'data', 'uploads/files2');

So I thought instead, I'd get all objects in a folder and copy each individually. But I'm only able to get objects in a container:

    $container = $this->connection->get_container('data');
    $files = $container->list_objects();

this doesn't work:

    $container = $this->connection->get_container('data/uploads');
    $files = $container->list_objects();

How can I rename a folder? Or alternatively, move all objects in a folder to a new one?

The issue here is that there is no such thing as a folder inside of a container. The container is the top-level grouping, and under that is just a set of objects.

Your objects have directory separators in their names so that they can be referred to that way in URLs. In reality, they are just individual objects with similar prefixes in their names; they are not actually grouped "by folder".

I'm not a PHP programmer, but here's some pseudocode (may not be exact):

Edited to use function provided by @magglass1

$old_dir = "oldir/";
$new_dir = "newdir/";
$container = $this->connection->get_container('data');
$files = $container->list_objects(0, NULL, $old_dir);
foreach ($files as $file) {
    $new_filename = substr_replace($file, $new_dir, strpos($file, $old_dir), strlen($old_dir));
    $this->container->move_object_to($file, 'data', $new_filename);
};

You can list objects by pseudo folder by specifying a prefix in your list_objects() call. Here is the usage of the function:

list_objects($limit=0, $marker=NULL, $prefix=NULL, $path=NULL)

So something along the lines of this should work:

$container = $this->connection->get_container('data');
$files = $container->list_objects(0, NULL, 'uploads/');

You'd then loop through and copy all the returned objects.

-Mark

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM