Search code examples
codeigniter-3azure-blob-storage

codeigniter3 : Listing all directory and files from azure blob storage?


I have account name, account key and other necessary information of azure blob storage for connect to azure blob storage. I want to list all directories and files from azure blob storage. I searched and found links but not understanding that how to integrate from start in CodeIgniter.

I search this library https://github.com/Azure/azure-sdk-for-php#readme

Can someone provide me link or tell me how to integrate in codeigniter from start?


Solution

  • If you want to list blobs under a container or some directory by cURL, just try the code below:

    <?php 
    
    function generateSharedAccessSignature($accountName, 
        $storageKey, 
        $signedPermissions, 
        $signedService, 
        $signedResourceType, 
        $signedStart, 
        $signedExpiry, 
        $signedIP, 
        $signedProtocol, 
        $signedVersion){
    
        if(empty($accountName)){
            trigger_error("The account name is required.");
            return;
        }
    
        if(empty($storageKey)){
            trigger_error("The account key is required.");
            return;
        }
    
        if(empty($signedPermissions)){
            trigger_error("The permissions are required.");
            return;
        }
    
        if(empty($signedService)){
            trigger_error("The services are required.");
            return;
        }
    
        if(empty($signedResourceType)){
            trigger_error("The resource types are required.");
            return;
        }
    
        if(empty($signedExpiry)){
            trigger_error("The expiration time is required.");
            return;
        }
    
        if(empty($signedVersion)){
            trigger_error("The service version is required.");
            return;
        }
        // generate the string to sign
        $_toSign = urldecode($accountName) . "\n" . 
                urldecode($signedPermissions) . "\n" . 
                urldecode($signedService) . "\n" . 
                urldecode($signedResourceType) . "\n" . 
                urldecode($signedStart) . "\n" .
                urldecode($signedExpiry) . "\n" .
                urldecode($signedIP) . "\n" .
                urldecode($signedProtocol) . "\n" .
                urldecode($signedVersion) . "\n";
    
        // sign the string using hmac sha256 and get a base64 encoded version_compare
        $_signature = base64_encode(hash_hmac("sha256", utf8_encode($_toSign), base64_decode($storageKey), true));
    
        return $_signature;
    }
    
    $key= "";
    $storageAccount = "";
    $containerName = "";
    $directoryName = ""; //leave it empty if list all blobs in container
    
    $_signedPermissions = "rl";  //read and list permission
    $_signedService = "b";       // for blob service
    $_signedResourceType = "c";  //only for access container 
    $_signedStart = "2021-05-31T00:00:00Z";  //sas token start time
    $_signedExpiry = "2021-06-05T00:00:00Z"; //sas token expairy time
    $_signedIP = NULL;     // no IP limit
    $_signedProtocol = "https";
    $_signedVersion = "2020-02-10";
    
    $_signature = generateSharedAccessSignature($storageAccount, 
        $key, 
        $_signedPermissions, 
        $_signedService, 
        $_signedResourceType, 
        $_signedStart, 
        $_signedExpiry, 
        $_signedIP, 
        $_signedProtocol, 
        $_signedVersion);
    
    $sig = urlencode($_signature);
    
    $destinationURL = "https://$storageAccount.blob.core.windows.net/$containerName?restype=container&comp=list&prefix=$directoryName&sp=$_signedPermissions&srt=$_signedResourceType&ss=$_signedService&st=$_signedStart&se=$_signedExpiry&sv=$_signedVersion&spr=$_signedProtocol&sig=$sig";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $destinationURL);
    
    $content = curl_exec($ch);
    echo $content;
    
    ?>
    

    Result:

    enter image description here

    enter image description here