0
votes

Is Azure SDK for PHP Blob Storage method works only on a Blob Storage account? Because whenever I tried to use it on an Azure General-purpose Storage account (which gives you access to Azure Storage services such as Tables, Queues, Files, Blobs and Azure virtual machine disks under a single account), my php program stops at an specific process in an inconsistent manner. At times it processes completely but when I re-run it, it stops again in a random point.

My goal is to get all blobs under my Azure subscription using foreach loop, given that I have an array variable that holds connection strings for each storage accounts. It works well with a Blob Storage account but whenever I include the General-purpose Storage account, the problem starts to occur.

I already tried overriding my php memory limit and still it does not solve the issue.

Any advise guys? Thank you in advance.

2
Are you using premium storage performance tier? And please also share any code that you have written to work with Azure Blob Storage.Aaron Chen
The Azure account I am using already has an existing storage account, both standard and premium (our departments shared Azure account). 33 out of 58 storage accounts are General-purpose and some of them are premium and some are standard. Generally, I wanted to get the details of all storage types. But as I read your question, it seems that you are pointing that the premium tier is the one causing the inconsistency?LBB
I tried your code with my General-purpose Storage account, it worked fine. Could you test your single General-purpose Storage account for that?Aaron Chen
It is working when I used it on a single General-purpose Storage account, but when I put it in more than one Storage account using a foreach loop, the process stops at any random container of a Storage account that has many containers in it.LBB
Have you tried using ini_set('memory_limit', '-1'); to set memory limit to unlimited?Aaron Chen

2 Answers

0
votes
//Here is my code for you to visualize my goal..

<!DOCTYPE html>
<?php
require_once "vendor/autoload.php";
include_once 'Dbcon.php';

use MicrosoftAzure\Storage\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Common\ServiceException;

if (($handle = fopen("csv/azurestorages.csv", "r")) !== FALSE) {
    fgetcsv($handle);
    $acc_cred = array();
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        for ($c=0; $c < $num; $c++) {
            $acc_cred[$data[0]][$c] = $data[$c];
        }
   }
}
fclose($handle);

$all_storage = array();

foreach($acc_cred as $ac){

    $connstr = "DefaultEndpointsProtocol=http;AccountName=$ac[0];AccountKey=$ac[1]";

    // Create blob REST proxy.
    $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connstr);

    // List all containers
    $container = $blobRestProxy->listContainers();
    $containers = $container->getContainers();

    if (count($containers) > 0) {

        $blob_xyzrobot = array();

        foreach($containers as $c){
            $blob_xyzrobot[] = $c->getName();
        }

        foreach($blob_xyzrobot as $bx)
        {
                $uri_segments = array();
                $blobstoragename = array();

                // List blobs.
                $blob_list = $blobRestProxy->listBlobs($bx);

                $blobs = $blob_list->getBlobs();

                if (count($blobs) > 0) {

                    $ctr = 0;
                    $uri_segments = explode('/', $blobs[0]->getUrl());
                    $blobstoragename = explode('.',$uri_segments[2]);

                    foreach($blobs as $blob)
                    {
                        $bprename = $blobstoragename[0];
                        $all_storage[$bprename."|".$bx][$ctr]['storage'] = $blobstoragename[0];
                        $all_storage[$bprename."|".$bx][$ctr]['container'] = $bx;
                        $all_storage[$bprename."|".$bx][$ctr]['whatdate'] = $blob->getProperties()->getLastModified()->format('Y-m-d');
                        $all_storage[$bprename."|".$bx][$ctr]['blob'] = $blob->getName();
                        $all_storage[$bprename."|".$bx][$ctr]['bytesize'] = $blob->getProperties()->getContentLength();
                        $all_storage[$bprename."|".$bx][$ctr]['datecreated'] = date("Y-m-d H:i:s");
                        $ctr++;
                    }
                }
        }
    }
}
if (count($all_storage) > 0){
   print_r($all_storage);
}else{
   echo "No blobs in found...";    
}

?>
0
votes
//And this is the powershell script that generates the csv file accessed by my php

    Get-AzureStorageAccount | foreach-object {
    Get-AzureStorageKey -StorageAccountName $_.StorageAccountName
    } | Select-object StorageAccountName, Primary, Secondary | Export-CSV "..csv\bs_asm.csv" -Encoding ascii -NoTypeInformation
    Start-Sleep 1
    Get-AzureRmStorageAccount | foreach-object {
    $Primary = (Get-AzureRmStorageAccountKey -Name $_.StorageAccountName -ResourceGroupName $_.ResourceGroupName).Value[0]
    $Secondary = (Get-AzureRmStorageAccountKey -Name $_.StorageAccountName -ResourceGroupName $_.ResourceGroupName).Value[1]
    New-Object -TypeName PSObject -Property @{
    StorageAccountName = $_.StorageAccountName
    Primary = $Primary
    Secondary = $Secondary
    } | Select-Object StorageAccountName, Primary, Secondary
    } | Export-CSV "..csv\bs_arm.csv" -Encoding ascii -NoTypeInformation
    $directory = "..csv\*.*";
    $csvFiles = Get-ChildItem -Path $directory -Filter *.csv;
    $content =@();
    foreach ($csv in $csvFiles) {
    $content += Import-Csv $csv;
    }
    $content | Export-Csv -Path "..csv\azureblobstorages.csv" -Encoding ascii -NoTypeInformation;