使用PHP从windows azure容器中获取所有文件

时间:2015-07-04 09:10:59

标签: php azure azure-storage azure-storage-blobs

如何使用php从windows azure容器中获取所有文件(blob)。

我使用下面的代码只获取500个blob,我想获取所有文件。

$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);


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

    foreach($blobs as $blob)
    {
        //echo $blob->getName().": ".$blob->getUrl()."<br /><br />";
        echo $blob->getUrl()."<br />";
        $photo_name=strtolower($blob->getName());
        //echo $rs=upload_own_image("raagaimg",$photo_name,$blob->getUrl());
        //echo "<br /><br />";
    }
}
catch(ServiceException $e){
    // Handle exception based on error codes and messages.
    // Error codes and messages are here: 
    // http://msdn.microsoft.com/library/azure/dd179439.aspx
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br />";
}

由于

Thanigaivelan

1 个答案:

答案 0 :(得分:4)

尝试以下代码。基本上,在单个存储服务调用中列出blob,最多返回5000个blob。如果容器中有超过5000个blob,则存储服务会返回一个延续令牌(称为nextMarker),您需要使用它来获取下一组blob。

<?php
require_once 'WindowsAzure.php';
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Blob\Models\SetBlobPropertiesOptions;
use WindowsAzure\Blob\Models\ListBlobsOptions;
try {
    $containerName = "container-name";
    $connectionString = 'DefaultEndpointsProtocol=http;AccountName=accountname;AccountKey=accountkey';
    $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString); 
    $nextMarker = "";
    $counter = 1;
do {
    $listBlobsOptions = new ListBlobsOptions();
    $listBlobsOptions->setMarker($nextMarker);
    $blob_list = $blobRestProxy->listBlobs($containerName, $listBlobsOptions);
    $blobs = $blob_list->getBlobs();
    $nextMarker = $blob_list->getNextMarker();
    foreach($blobs as $blob) {
        echo $blob->getUrl()."\n";
        $counter = $counter + 1;
    }
    echo "NextMarker = ".$nextMarker."\n";
    echo "Files Fetched = ".$counter."\n";
} while ($nextMarker != "");
echo "Total Files: ".$counter."\n";
}
catch(Exception $e){
$code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br />";
}  
?>