Search code examples
powershellazure-devopshttpsazure-queuesazurite

Create queue in Azurite via PowerShell script


I want to have a script to create a queue in Azurite running in a docker container. (Preferably a PowerShell script)

My docker-compose.yml: enter image description here

My running container info: enter image description here

I don't quite understand how I can achieve creation of a queue. This is the official documentation. And here's my current code in my PowerShell script:

# $url = "https://127.0.0.1:10001/devstoreaccount1/newqueue";
$url = "https://0.0.0.0:10001/devstoreaccount1/newqueue";

# default account credentials
$auth = "SharedKey devstoreaccount1:Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
$date = "Tue, 21 Dec 2021 23:39:12 GMT"

# required headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',$auth)
$headers.Add('x-ms-date',$date)

Invoke-RestMethod -Method PUT -Uri $url -Headers $headers;

I can't get this working neither with https://127.0.0.1:10001 nor with https://0.0.0.0:10001. Here are the corresponding errors I get when trying one & the other: enter image description here I get same first error when trying to send http request as well http://127.0.0.1:10001. Anyway, I need to use https.

Please help me send a proper request to Azurite running in container to create a queue.


Solution

  • Posting this answer, just in case someone will need it one day. I've ended up creating a small .NET console application & calling it in PowerShell.

    # Default Azurite account credentials
    $ACCOUNT_NAME = 'devstoreaccount1'
    $ACCOUNT_KEY = 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=='
    $QUEUE_NAME = 'queue-name'
    
    dotnet run --project ..\Scripts\CreateQueues -- $ACCOUNT_NAME $ACCOUNT_KEY $QUEUE_NAME
    

    And in console app what you have to do is to configure a queue client on creation and call Create method on it.

    // Parse the args
    // queueName = "queue-name"
    // accountName = "devstoreaccount1"
    // accountKey = "# Default Azurite account credentials
    var baseUrl = "https://localhost:10001/devstoreaccount1/";
    var requestUri = baseUrl + queueName;
    var queueClient = QueueClient(new Uri(requestUri), new StorageSharedKeyCredential(accountName, accountKey));
    queueClient.Create(); // This may throw an exception. Make sure to handle those cases.