1
votes

I am trying to list the containers and so far having no luck at all... i already tried

$aBlobContainer = $blobRestProxy->listContainers();

for($i = 0;$i<= count($aBlobContainer); $i++)
{
 echo 'Blob Container name is: '.$aBlobContainer[$i]->Name."\n";
}

but i am having error

Cannot use object of type WindowsAzure\Blob\Models\ListContainersResult as array

Been trying to work around it all day just can't seem to make any progress... let me know if i am doing something silly or if there is a better way to find out if the container already exist? Thanks!

EDIT:

var_dump of the variable $aBlobContainer came up as


    object(WindowsAzure\Blob\Models\ListContainersResult)#42 (5) {
      ["_containers":"WindowsAzure\Blob\Models\ListContainersResult":private]=>
      array(2) {
        [0]=>
        object(WindowsAzure\Blob\Models\Container)#48 (4) {
          ["_name":"WindowsAzure\Blob\Models\Container":private]=>
          string(6) "abc123"
          ["_url":"WindowsAzure\Blob\Models\Container":private]=>
          string(48) "http://orig.blob.core.windows.net/abc123"
          ["_metadata":"WindowsAzure\Blob\Models\Container":private]=>
          array(0) {
          }
          ["_properties":"WindowsAzure\Blob\Models\Container":private]=>
          object(WindowsAzure\Blob\Models\ContainerProperties)#47 (2) {
            ["_lastModified":"WindowsAzure\Blob\Models\ContainerProperties":private]=>
            object(DateTime)#49 (3) {
              ["date"]=>
              string(19) "2012-11-29 01:32:20"
              ["timezone_type"]=>
              int(2)
              ["timezone"]=>
              string(3) "GMT"
            }
            ["_etag":"WindowsAzure\Blob\Models\ContainerProperties":private]=>
            string(19) ""0x8CF9BE88256926F""
          }
        }
        [1]=>
        object(WindowsAzure\Blob\Models\Container)#46 (4) {
          ["_name":"WindowsAzure\Blob\Models\Container":private]=>
          string(8) "multi123"
          ["_url":"WindowsAzure\Blob\Models\Container":private]=>
          string(50) "http://orig.blob.core.windows.net/multi123"
          ["_metadata":"WindowsAzure\Blob\Models\Container":private]=>
          array(0) {
          }
          ["_properties":"WindowsAzure\Blob\Models\Container":private]=>
          object(WindowsAzure\Blob\Models\ContainerProperties)#45 (2) {
            ["_lastModified":"WindowsAzure\Blob\Models\ContainerProperties":private]=>
            object(DateTime)#53 (3) {
              ["date"]=>
              string(19) "2012-11-29 03:13:16"
              ["timezone_type"]=>
              int(2)
              ["timezone"]=>
              string(3) "GMT"
            }
            ["_etag":"WindowsAzure\Blob\Models\ContainerProperties":private]=>
            string(19) ""0x8CF9BF69C25759F""
          }
        }
      }
      ["_prefix":"WindowsAzure\Blob\Models\ListContainersResult":private]=>
      NULL
      ["_marker":"WindowsAzure\Blob\Models\ListContainersResult":private]=>
      NULL
      ["_nextMarker":"WindowsAzure\Blob\Models\ListContainersResult":private]=>
      NULL
      ["_maxResults":"WindowsAzure\Blob\Models\ListContainersResult":private]=>
      NULL
    }

3

3 Answers

3
votes

Looking at the Source Code:

$blobContainers = $blobRestProxy->listContainers(); //returns ListContainersResult

in order to get the listing of containers you'd have to do a subsequent call of:

$blobContainerArray = $blobContainers->getContainers(); //exposes the array of containers

Then you should be able to use that array in either a foreach or for statement. This workflow matches that of returning a list of blobs from within a container as seen in the README.md file:

try {
    // List blobs.
    $blob_list = $blobRestProxy->listBlobs("mycontainer");
    $blobs = $blob_list->getBlobs();

    foreach($blobs as $blob)
    {
        echo $blob->getName().": ".$blob->getUrl()."<br />";
    }
} catch(ServiceException $e){
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br />";
}
0
votes
$options = new ListContainersOptions();
$options->setPrefix("prefixxxx");
$blobContainers = $blobRestProxy->listContainers($options);
$blobContainerArray = $blobContainers->getContainers();

foreach ($blobContainerArray as $container) 
{
    Trace("Container: " . $container->getName());
}
-1
votes

From the error message, it looks like $blobRestProxy->listContainers() is returning an object. Try the code below.

$aBlobContainer = $blobRestProxy->listContainers();

foreach($aBlobContainer as $row) {
    echo 'Blob Container name is: '.$row->Name."\n";
}

When accessing $aBlobContainer as an array (i.e. $aBlobContainer[$i]), it was probably giving the error.

* Edit *

foreach($aBlobContainer as $key => $row) {
  echo $row->Name . "\n";
}