首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从RackSpace文件下载文件

从RackSpace文件下载文件
EN

Stack Overflow用户
提问于 2013-11-06 20:17:30
回答 2查看 4.4K关注 0票数 2

我已经使用composer安装了PHP。“示例”文件夹似乎有旧代码,因为它不适合我。我想从RackSpace文件夹下载一些文件。以下是我的代码,它没有带来任何东西。

代码语言:javascript
复制
<?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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-08 18:47:40

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

接下来,包含对象存储的示例代码位于here,但不包括您想要做的事情。

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

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

代码语言:javascript
复制
<?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";
                }
            }
        }
    }
}

输出:

代码语言:javascript
复制
Containers:
*** gallery
*** test2
** 61OUUC44G-L._SL1471_.jpg
* Saving object
* File written

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

** accessibility2.jpg
* Saving object
* File written

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

代码语言:javascript
复制
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
票数 8
EN

Stack Overflow用户

发布于 2015-05-27 09:40:20

递归下载Rackspace云文件

代码语言:javascript
复制
<?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);
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19821946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档