2

我已经使用 Composer 安装了 PHP SDK。似乎示例文件夹有旧代码,因为它对我不起作用。我想从 RackSpace 文件夹中下载一些文件。以下是我的代码,它什么也没带来。

<?php
require 'vendor/autoload.php';

$authURL = 'https://identity.api.rackspacecloud.com/v2.0/';
$credentials = array(
    'username' => 'XXXX',
    'apiKey' => 'XXXX'
);
$connection = new \OpenCloud\Rackspace($authURL, $credentials);
// var_dump($connection);exit;
$objstore = $connection->objectStoreService('cloudFiles', 'DFW');

// get our containers
print("Containers:\n");
$conlist = $objstore->listContainers();
//var_dump($conlist);
while($container = $conlist->Next()) {
    printf("* %s\n", $container->name);
}
4

2 回答 2

8

首先,更新到php-opencloud的最新版本,目前是 1.7。

接下来,包含的对象存储示例代码位于此处,但不包含您想要执行的操作。

给定路径的以下代码将遍历您的容器并将对象从您的容器保存到目标路径 ($savePath)。如果该对象已经存在于该路径中,它将被跳过。此版本包括指示每个对象成功或失败的输出。试一试,如果您有任何问题,请告诉我。

注意:请记住,Rackspace 的云文件对象存储是在每个数据中心的基础上处理的,因此存储在 ORD 中的容器中的文件只有在您连接到 ORD 中的 objectStoreService 时才能访问。

<?php
require 'vendor/autoload.php';

$authURL = 'https://identity.api.rackspacecloud.com/v2.0/';
$credentials = array(
    'username' => 'YOUR_USERNAME',
    'apiKey' => 'YOUR_API_KEY',
);

$savePath = '/path/to/files/';
$connection = new \OpenCloud\Rackspace($authURL, $credentials);

$objstore = $connection->objectStoreService('cloudFiles', 'ORD');

// get our containers
print("Containers:\n");
$conlist = $objstore->listContainers();
//var_dump($conlist);
while($container = $conlist->Next()) {
    printf("*** %s\n", $container->name);
    if($container->name == 'test2')
    {
        $files = $container->ObjectList();
        while($o = $files->Next())
        {
            $file_name = $o->getName();
            // Get our object
            $file = $container->getObject($file_name);
            printf("** %s\n", $file->getName());
            // Let's save this file
            echo "* Saving object\n";
            if(file_exists($savePath.$file_name))
            {
                echo "* File already exists! SKIPPING\n\n";
            }
            else
            {
                if (!$fp = @fopen($savePath.$file_name, "wb")) {
                    throw new OpenCloud\Common\Exceptions\IOError(sprintf(
                        'Could not open file [%s] for writing',
                        $savePath.$file_name
                    ));
                }
                //$retval = fwrite($fp, $o->getContent());
                if (fwrite($fp, $file->getContent()) === FALSE) {
                    echo "* ERROR - Cannot write to file ($savePath.$file_name)\n\n";
                }
                else
                {
                    echo "* File successfully written\n\n";
                }
            }
        }
    }
}

输出:

Containers:
*** gallery
*** test2
** 61OUUC44G-L._SL1471_.jpg
* Saving object
* File written

** Computer-Code.jpg
* Saving object
* File written

** accessibility2.jpg
* Saving object
* File written

我的服务器上的目录列表:

root@app01:/path/to/files# ll
total 960
drwxrwxrwx  2 root     root       4096 Nov  8 18:53 ./
drwxr-xr-x 15 www-data www-data   4096 Nov  8 18:20 ../
-rw-r--r--  1 www-data www-data  68650 Nov  8 18:45 61OUUC44G-L._SL1471_.jpg
-rw-r--r--  1 www-data www-data 374177 Nov  8 18:45 accessibility2.jpg
-rw-r--r--  1 www-data www-data 515919 Nov  8 18:45 Computer-Code.jpg
于 2013-11-08T18:47:40.453 回答
2

递归下载 Rackspace 云文件

<?php
/**
 *  "require": {
 *      "rackspace/php-opencloud": "dev-master"
 *  }
 */

ini_set('memory_limit', '2048M'); // size must be bigger than the biggest file
ini_set('max_execution_time', 0);

require 'vendor/autoload.php';

use OpenCloud\Rackspace;

// Instantiate a Rackspace client.
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
    'username' => '<USERNAME>',
    'apiKey'   => '<APIKEY>'
));

$objStore = $client->objectStoreService('cloudFiles', 'LON');
$savePath = __DIR__.'/backup/';

// get our containers
print("Containers:\n");
$containersList = $objStore->listContainers();

while($container = $containersList->Next()) {
    if( ! in_array($container->name, array('.CDN_ACCESS_LOGS', '<CONTAINER_TO_EXCLUDE>'))) {
        printf("*** %s\n", $container->name);

        $containerDir = $savePath.$container->name.'/';
        if (!is_dir($containerDir)) {
            mkdir($containerDir, 0777, true);
        }

        $files = $container->ObjectList();
        while($o = $files->Next()) {
            $file_name = $o->getName();

            if (file_exists($containerDir . $file_name)) {
                echo '## '.$containerDir.$file_name.' already exists'."\n";
                continue;
            }

            // Get our object
            $file = $container->getObject($file_name);

            if (strpos($file->getName(), '<FILES_TO_EXCLUDE>') !== false) {
                continue;
            }

            $tempDir = $containerDir . dirname($file->getName()) . '/';    
            if (!is_dir($tempDir)) {
                mkdir($tempDir, 0777, true);
            }

            if (file_put_contents($containerDir . $file_name, $file->getContent())) {
                printf("** %s - OK\n", $file->getName());
            } else {
                printf("** %s - KO\n", $file->getName());
            }
            unset($file);
        }
    }
}
于 2015-05-27T09:40:20.753 回答