2

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?

Nirdesh Kumawat
  • 386
  • 2
  • 16
  • 56
  • take a look at this [link](https://github.com/thomasantony/codeigniter-azure) and this [answer](https://stackoverflow.com/questions/41586511/how-to-use-codeigniter-to-upload-image-to-azure-blob-storage) to see if it helps. – Ivan Glasenberg Nov 27 '20 at 01:04
  • can you solve the issue now? – Ivan Glasenberg Dec 01 '20 at 00:41
  • @IvanYang I used code of above link and when I call in controller `$blob_storage -> listBlobs` after load Azure library then I got a error `Call to a member function get_blob_storage() on null` – Nirdesh Kumawat May 27 '21 at 11:31

3 Answers3

2

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

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16
  • @Martin , does this helpful? https://stackoverflow.com/questions/28858351/php-ssl-certificate-error-unable-to-get-local-issuer-certificate – Stanley Gong Jun 01 '21 at 07:46
0

Consider reading this example https://learn.microsoft.com/en-us/azure/storage/blobs/storage-upload-process-images?tabs=dotnet it is quite informative and should be able to helo you , incase you have any further questions i will be happy to assist you

0

Here is an link about codeigniter-azure, you can take a look at it.

There is also an SO issue about it.

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60