从RackSpace文件下载文件

神秘

我已经使用composer安装了PHP SDK。看来samples文件夹中有旧代码,原因对我来说不起作用。我想从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);
}
克里斯·拉斯科(Chris Rasco)

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

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

以下给出路径的代码将遍历容器,并将对象从容器保存到目标路径($ savePath)。如果该路径中已经存在该对象,则将跳过该对象。此版本包含指示每个对象成功或失败的输出。试试看,让我知道是否有任何问题。

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

<?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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章