Search code examples
azure-pipelinesazure-storageazure-data-lakeazure-cliazure-data-lake-gen2

is there any way to create the AzureBlob containers and direcories inside a storage account using azure cli method?


Looking for an azurecli task or arm template to create blobs and directories within the blob inside existing Datalake Storage account gen2 , only if the given path is not existing. Looking to automate this task to Azurepipeline , where i can first verify and create the directory path within the storage account and create it if not present. then I have another azure cli task to set the permisiions within them. So llooking for the first task which is implemeneted at first to create the blobs and directory pasth if it not existing.


Solution

  • You can use powershell script in the Azure CLI task to check if container or directory exists and create them

    And the inline script can be as follows:

    $existing_container = (az storage container exists --account-name mystorageccount --name mycontainer  --auth-mode login | ConvertFrom-Json).exists
    $existing_directory = (az storage blob directory exists  --directory-path directoryName --account-name mystorageccount --container-name mycontainer  --auth-mode login | ConvertFrom-Json).exists
    if (!$existing_container)
                       az storage container create --account-name mystorageccount --name mycontainer  --auth-mode login
    if (!$existing_directory)
                       az storage blob directory create --account-name mystorageccount --container-name mycontainer  --directory-path directoryName --auth-mode login