简体   繁体   English

重命名Rackspace文件夹/容器

[英]Renaming Rackspace Folder/Container

I have the PHP library from Rackspace. 我有Rackspace的PHP库。 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. 您的对象名称中包含目录分隔符,以便可以在URL中以这种方式引用它们。 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): 我不是PHP程序员,但是这里有一些伪代码(可能不准确):

Edited to use function provided by @magglass1 编辑以使用@ 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. 您可以通过在list_objects()调用中指定前缀来按伪文件夹列出对象。 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 -标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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