gpt4 book ai didi

php - 使用 PHP 从 Windows Azure 容器获取所有文件

转载 作者:行者123 更新时间:2023-12-03 00:43:43 25 4
gpt4 key购买 nike

如何使用 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 />";
}

谢谢

塔尼盖伊兰

最佳答案

尝试下面的代码。本质上,在对列出 blob 的存储服务的单次调用中,最多返回 5000 个 blob。如果容器中的 Blob 数量超过 5000 个,存储服务会返回一个连续 token (称为 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 />";
}
?>

关于php - 使用 PHP 从 Windows Azure 容器获取所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31218948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com