我想从 php 在 azure 存储中创建目录和子目录并获取该目录。这是我的代码。
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connection_string);
$content = fopen("abc.txt", "r");
$blob_name = "dir1/dir2/di3/myblob";
try {
//Upload blob
$blobRestProxy->createBlockBlob("mycontainer", $blob_name, $content);
}
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 />";
}
我想获取目录“dir1”。我检查了该示例 How to create a sub container in azure storage location 。 但它不是在 php 中。任何人都可以帮助我如何从 PHP 代码创建并获取 azure 中的目录。
最佳答案
您可以使用ListBlobsOptions
从容器中查询 Blob 时设置特殊对象。根据您的要求,您可以使用setDelimiter('/')
使用此选项设置分隔符以将虚拟目录与 blob 名称分开。
并使用getBlobPrefixes()
从 ListBlobsResult
获取虚拟目录名称这是 listBlobs()
函数的结果。
请考虑以下代码片段:
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
$listBlobsOptions = new ListBlobsOptions();
$listBlobsOptions->setDelimiter('/');
$result = $blobClient->listBlobs(<container_name>, $listBlobsOptions);
foreach ($result->getBlobPrefixes() as $k => $b) {
//an instance of MicrosoftAzure\Storage\Blob\Models\BlobPrefix
array_push($return['blobPrefixes'], $b->getName());
}
以下是有关如何使用 Azure Staoge SDK for PHP 中的模块的示例。您可以从 https://github.com/Azure-Samples/storage-blob-php-webapplication/blob/master/PHP/api/trial.php#L163 全面了解使用情况.
如有任何进一步的疑问,请随时告诉我。
关于php - 在php中获取azure存储中的Blob目录名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39762591/