3

I have installed PHP SDK using composer. It seems samples folder has old code cause it does not work for me. I want to download some of files from RackSpace folder. Following is my code and it brings nothing.

<?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);
}
mysterious
  • 1,450
  • 5
  • 29
  • 56
  • Which version of php-opencloud are you using? – JRP Nov 06 '13 at 20:29
  • 1.7- Downloaded it yesterday with composer. "rackspace/php-opencloud": "dev-master" – mysterious Nov 06 '13 at 20:32
  • 1
    OP, I did the git clone install method and just ran your code and I see a listing of all of my containers. Do you have display_errors enabled? – Chris Rasco Nov 06 '13 at 21:17
  • Actually you are right. Code should work for you. It worked for me after I changed DFW with ORD. Can you make your comment a reply so that I can accept it. It will be awesome if you can tell in your answer that how can I download container. – mysterious Nov 07 '13 at 14:01
  • I'll work on that OP and post as an answer for you. – Chris Rasco Nov 07 '13 at 20:13

2 Answers2

9

First and foremost, update to the latest release of php-opencloud, currently 1.7.

Next, included sample code for the Object Store is located here, but doesn't include what you were looking to do.

The following code, given a path, will iterate through your containers and save the objects from your container to the destination path ($savePath). If the object already exists in that path, it will be skipped. This version includes output indicating success or failure for each object. Give this a try and let me know if you have any issues.

NOTE: Keep in mind that Rackspace's Cloud Files, Object Store, is handled on a per Datacenter basis so files stored in a container in ORD would be accessible only if you connect to the objectStoreService in 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";
                }
            }
        }
    }
}

Output:

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

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

** accessibility2.jpg
* Saving object
* File written

Directory Listing on my server:

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
Chris Rasco
  • 2,713
  • 1
  • 18
  • 22
2

Download Rackspace Cloud Files recursively

<?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);
        }
    }
}
Manuel K.
  • 101
  • 4
  • Don't forget to update the second parameter for "$client->objectStoreService" to the region location of your container(s) as found on https://mycloud.rackspace.com/ at "Storage > Files", e.g. ORD, IAD, etc. – Jarrett Barnett Sep 07 '17 at 22:11