我已经使用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);
}
答案 0 :(得分:7)
首先,请更新到最新版本的php-opencloud,目前为1.7。
接下来,对象商店中包含的示例代码位于here,但不包括您要查看的内容。
给定路径的以下代码将遍历您的容器并将容器中的对象保存到目标路径($ 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
答案 1 :(得分:2)
递归下载Rackspace Cloud Files
<?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);
}
}
}