Search code examples
azureazure-storageazure-blob-storage

Get all the blobs inside sub directory inside container


So I'm having issues on getting the blobs inside my container - I'm able to successfully grab all the blobs inside a container, but not inside a folder in the container - How would I be able to successfully do that?

Problem:
I have a folder inside my container called 'big/' - How would I be able to grab all the blobs from that folder instead of just the generic container as I'm doing below?

All help would be appreciated!

So here is the getBlobs() method that I have (View the image and code below):
enter image description here

/**
 * Get all blobs from container
 *
 * @param int $max
 * @return array
 */
public function getBlobs(int $max = 5000) : array
{
    // Check if a container is set
    if (!$this->containerName) {
        return [];
    }

    try {
        $blobMeta = [];
        $blobOptions = new ListBlobsOptions();

        // Set the max results at 5000 (Default: 5000)
        $blobOptions->setMaxResults($max);

        do {
            // Grab the defined container
            $blob_list = $this->connect()->listBlobs(
                $this->containerName,
                $blobOptions
            );

            var_dump($blob_list);
            die();

            // Loop through all the
            foreach ($blob_list->getBlobs() as $blob) {
                $blobMeta[] = [
                    'name' => $blob->getName(),
                    'url' => $blob->getUrl(),
                ];
            }
            $blobOptions->setContinuationToken($blob_list->getContinuationToken());

        } while ($blob_list->getContinuationToken());

        return $blobMeta;
    } catch (ServiceException $e) {
        $code = $e->getCode();
        $error_message = $e->getMessage();
        echo $code . ": " . $error_message . PHP_EOL;
        return [];
    }
}

Solution

  • If you want to list blobs under a folder, you can use ListBlobsOptions to setPrefix("YourFolderName/") to do this. You can find a detailed code sample in listBlobsSample function at line 430 here.