# Azure

Two of Azure's services, from the back end, in ordinary Mote: Blob Storage
(every request signed with the account's key, SharedKey; addresses for a
browser, a service SAS), with the calls the [s3](s3.md) extension has where
they mean the same; and Communication Services Email behind
[mail](mail.md)'s `mailSend`. An [extension](../extensions/README.md):
`extensions/azure/`. Back to the [map](README.md).

```text
use "pkg:azure/blob.mote"

server fn keepInvoice(order: string, pdf: bytes): string requires signedIn
  azurePut(`invoices/${order}.pdf`, pdf, "application/pdf")?
  return azurePresignGet(`invoices/${order}.pdf`, 3600, `invoice-${order}.pdf`)?.url
```

**What was checked.** SharedKey (a PUT with its `x-ms-` headers, a list
with its query), a service SAS, and Communication Services' HMAC, each
against the same signature worked out apart from this code by Node's
crypto from the strings to sign Microsoft documents; the requests and the
answers (a list in two pages, a refusal's code) with `given net`, in
`extensions/azure/tests.mote` (`tests/all.sh real_svc_azure`). Microsoft
publishes no example key and signature to compare with, and there is no
stand-in server: **nothing here has met Azure, nor Azurite**.

## The settings

| | |
|---|---|
| `AZURE_STORAGE_ACCOUNT` | The storage account. |
| `AZURE_STORAGE_KEY` | One of its keys, base64 (a [secret](back-end.md#secrets)). |
| `AZURE_CONTAINER` | The container. |
| `AZURE_BLOB_URL` | Another address: Azurite's `http://127.0.0.1:10000/devstoreaccount1`. |
| `AZURE_COMMUNICATION_ENDPOINT`, `AZURE_COMMUNICATION_KEY` | With `MAIL_PROVIDER=azure`: the Communication Services resource and its access key (a secret). `MAIL_FROM` an address of a domain connected to it. |

## The calls

| Call | |
|---|---|
| `azurePut(name, data, type)` | A block blob, in one request (5000 MiB at most). |
| `azureGet(name)` · `azureHead(name)` · `azureDelete(name)` | Its bytes (kind `"missing"` where none); what is kept there, or none; taken away. |
| `azureList(prefix, most)` | The names under a prefix, page by page (List Blobs, by its `NextMarker`). |
| `azurePresignGet(name, seconds, download)` · `azurePresignPut(name, seconds, type)` | A service SAS for one blob: read, or create and write (the browser sends `x-ms-blob-type: BlockBlob`). A SAS cannot hold a browser to a type or a size: check `azureHead` after. |
| `azureSign(…)`, `azureSas(…)` | The signatures, for a call not written out here. |
| `mailSend` with `MAIL_PROVIDER=azure` | `POST /emails:send?api-version=2023-03-31`, signed with HMAC-SHA256; accepted (202) and sent later: the id is the operation's. |

Failures have a kind: `"missing"`, `"auth"`, `"conflict"`, `"limited"`
(ServerBusy), `"invalid"`, `"azure"`, `"offline"`; `data.code` is
Azure's `x-ms-error-code`.

**Unsure**: `x-ms-version` 2023-11-03 and the SAS's sixteen fields for it,
and the Email API's version, as of 2026: each is in one place in the file.

## What else of Azure would matter

Not built: **Entra ID tokens** (a managed identity's token from the
instance metadata service, `http://169.254.169.254/metadata/identity/oauth2/token`,
or a service principal's client-credentials token): the way a back end on
Azure is meant to reach Storage without a key, and what every other Azure
service takes. Then **Service Bus** (queues, AMQP or its REST API),
**Cosmos DB** (its own HMAC auth, or Entra), **Key Vault** secrets, **Azure
OpenAI** (an OpenAI-style API: the ai extension's `openai` provider with
`AI_API` and an `api-key` header would need a line), **Event Grid**
(Communication Services' delivery reports and bounces come that way), and
user-delegation SAS (signed with an Entra token, not the account key).

## Checking it against Azure

1. Azurite first (`AZURE_BLOB_URL=http://127.0.0.1:10000/devstoreaccount1`,
   its well-known account and key): put, head, get, list of a dozen with
   `most: 5`, delete; a presigned GET in a browser.
2. A storage account and a container in a test subscription: the same, then
   a presigned PUT from a page, and the same SAS after its time (403).
3. Communication Services with an Azure-managed domain: `mailSend` to your
   own address, with and without a file.
