1

I want to create directory and sub directory in azure storage from php and get that directory. This is my code.

$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 />";
}

I want to get directory "dir1".I checked that example How to create a sub container in azure storage location. But it's not in php. Can any one help me how can we create and get directory in azure from PHP code.

Community
  • 1
  • 1
ankit
  • 1,114
  • 1
  • 19
  • 35
  • Would you mind explaining what you mean by `how can we create and get directory in azure from PHP code.`? – Gaurav Mantri Sep 29 '16 at 06:39
  • Means i want to create directory and in this many sub directory. As you look in the given link "content/blue/images/logo.jpg, content/blue/images/icon-start.jpg, content/red/images/icon-stop.jpg" and get this blobs with directory name. As example show in link "CloudBlobDirectory directory = cloudBlobClient.GetBlobDirectoryReference(directoryName); " But it's not in php – ankit Sep 29 '16 at 06:45
  • So one thing is that blob storage doesn't support sub directories. As such you can't create a sub directory inside a container. You essentially create an illusion of a subdirectory by prefixing your blob name. In the example above, `content/red/images` is a blob prefix and the name of the blob is ` content/red/images/icon-stop.jpg`. That leaves searching for blobs in a directory. What you can do is perform search by prefix where you specify the name of the prefix and storage service returns all blobs that start with that prefix. – Gaurav Mantri Sep 29 '16 at 06:54
  • So how can we use prefix name for getting all blobs? – ankit Sep 29 '16 at 06:59
  • You can specify the prefix in `ListBlobsOptions` (https://github.com/Azure/azure-storage-php/blob/master/src/Blob/Models/ListBlobsOptions.php). – Gaurav Mantri Sep 29 '16 at 07:22

1 Answers1

0

You can use ListBlobsOptions to set specially object when querying Blobs from container. Per your requirement, you can use the setDelimiter('/') of this options to set the delimiter as to separate the virtual directory from blob names.

And to use getBlobPrefixes() to get the virtual directory names from ListBlobsResult which is the result of listBlobs() function.

Please consider the following code snippet:

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());
}

And here is a sample about how to use the modules in Azure Staoge SDK for PHP. You can have a full glance of the usage from https://github.com/Azure-Samples/storage-blob-php-webapplication/blob/master/PHP/api/trial.php#L163.

Any further concern, please feel free to let me know.

Gary Liu
  • 13,758
  • 1
  • 17
  • 32