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

Tha*_*lan 1 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 />";
}
Run Code Online (Sandbox Code Playgroud)

谢谢

塔尼盖维兰

Gau*_*tri 5

试试下面的代码。本质上,在为列出 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 />";
}  
?>
Run Code Online (Sandbox Code Playgroud)