表示式狀態傳輸 (REST) API 是支援不同 HTTP 作業集(或方法)的服務端點。 這些 HTTP 方法可讓您針對服務的資源執行不同的動作。 只有在沒有現有的 az rest可以使用的情況下,才應該使用命令。
本文示範用來管理 Azure Container Registry 資源的 PUT、PATCH、GET、POST 和 DELETE HTTP 要求。 Azure Container Registry 是受控登錄服務,可讓您建立和維護儲存容器映射和相關成品的 Azure 容器登錄。
先決條件
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱開始使用 Azure Cloud Shell。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 如果您正在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 若要完成驗證程式,請遵循終端機中顯示的步驟。 如需其他登入選項,請參閱 使用 Azure CLI 向 Azure 進行驗證。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能的詳細資訊,請參閱 使用和管理 Azure CLI 的擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
使用秘訣 az rest
以下是使用 az rest 時的一些實用資訊:
-
az rest命令會自動使用登入認證進行驗證。 - 如果未設定授權標頭,它會附加標頭
Authorization: Bearer <token>,其中<token>會從 Microsoft Entra ID 擷取。 - 當
--url參數的開頭是--url命令輸出的端點時,令牌的目標資源將從az cloud show --query endpoints參數衍生而來。--url是必要參數。 - 使用
--resource參數來自訂資源。 - 如果未設定 Content-Type 標頭且
--body是有效的 JSON 字串,則 Content-Type 標頭將預設為 “application/json”。 - 使用
--uri-parameters的 OData 形式要求時,請務必在不同的環境中轉義$:在Bash中,將$轉義為\$;在PowerShell中,將$轉義為`$。
使用 PUT 建立 Azure Container Registry
使用 PUT HTTP 方法來建立新的 Azure Container Registry。
# Command format example
az rest --method put \
--url https://management.azure.com/subscriptions/<subscriptionId>/resourceGroups/<resourceGroup>/providers/Microsoft.ContainerRegistry/registries/<containerRegistryName>?api-version=2023-01-01-preview \
--body "{'location': '<locationName>', 'sku': {'name': '<skuName>'}, 'properties': {'adminUserEnabled': '<propertyValue>'}}"
以下是具有已完成參數的範例:
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
subscriptionId="00000000-0000-0000-0000-000000000000"
resourceGroup="msdocs-app-rg$randomIdentifier"
containerRegistryName="msdocscr$randomIdentifier"
locationName="westus"
skuName="Standard"
propertyValue="true"
# Create resource group
az group create --name $resourceGroup --location $locationName --output json
# Invoke request
az rest --method put \
--url https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.ContainerRegistry/registries/$containerRegistryName?api-version=2023-01-01-preview \
--body "{'location': '$locationName', 'sku': {'name': '$skuName'}, 'properties': {'adminUserEnabled': '$propertyValue'}}"
Bash 和 Powershell 的 JSON 輸出:
{
"id": "/subscriptions/<subscriptionId>/resourceGroups/<resourceGroup>/providers/Microsoft.ContainerRegistry/registries/<containerRegistryName>",
"location": "<location>",
"name": "<containerRegistryName>",
"properties": {
"adminUserEnabled": true,
"anonymousPullEnabled": false,
"creationDate": "2024-01-03T18:38:36.7089583Z",
"dataEndpointEnabled": false,
"dataEndpointHostNames": [],
"encryption": {
"status": "disabled"
},
"loginServer": "<containerRegistryName>.azurecr.io",
"networkRuleBypassOptions": "AzureServices",
"policies": {
"azureADAuthenticationAsArmPolicy": {
"status": "enabled"
},
"exportPolicy": {
"status": "enabled"
},
"quarantinePolicy": {
"status": "disabled"
},
"retentionPolicy": {
"days": 7,
"lastUpdatedTime": "2024-01-03T19:44:53.9770581+00:00",
"status": "disabled"
},
"softDeletePolicy": {
"lastUpdatedTime": "2024-01-03T19:44:53.9771117+00:00",
"retentionDays": 7,
"status": "disabled"
},
"trustPolicy": {
"status": "disabled",
"type": "Notary"
}
},
"privateEndpointConnections": [],
"provisioningState": "Succeeded",
"publicNetworkAccess": "Enabled",
"zoneRedundancy": "Disabled"
},
"sku": {
"name": "Standard",
"tier": "Standard"
},
"systemData": {
"createdAt": "2024-01-03T18:38:36.7089583+00:00",
"createdBy": "<username>@microsoft.com",
"createdByType": "User",
"lastModifiedAt": "2024-01-03T19:44:53.684342+00:00",
"lastModifiedBy": "<username>@microsoft.com",
"lastModifiedByType": "User"
},
"tags":{},
"type": "Microsoft.ContainerRegistry/registries"
}
使用 PATCH 更新您的 Azure Container Registry
使用 PATCH HTTP 要求更新您的 Azure Container Registry。 編輯 --body 參數,將其屬性更新為您想要的值。 此範例會使用上一節中設定的變數,並更新 Azure Container Registry 的 SKU 名稱($skuName=“Premium”。
#Variable Block
$skuName="Premium"
az rest --method patch \
--url https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.ContainerRegistry/registries/$containerRegistryName?api-version=2023-01-01-preview \
--body "{'location': '$locationName', 'sku': {'name': '$skuName'}, 'properties': {'adminUserEnabled': '$propertyValue'}}"
下列 JSON 字典輸出有省略的欄位,以求簡潔:
{
"id": "/subscriptions/<subscriptionId>/resourceGroups/<resourceGroup>/providers/Microsoft.ContainerRegistry/registries/<containerRegistryName>",
"location": "westus",
"name": "<containerRegistryName>",
"properties": {...},
"sku": {
"name": "Premium",
"tier": "Premium"
},
"systemData": {...},
"type": "Microsoft.ContainerRegistry/registries"
}
使用 GET 來取得您的 Azure Container Registry
使用 GET HTTP 請求查看 PATCH 請求的更新結果。 此範例會使用上一節中設定的變數。
az rest --method get \
--url https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.ContainerRegistry/registries/$containerRegistryName?api-version=2023-01-01-preview
GET 方法的輸出與 PUT 所示的輸出相同。
使用 POST 重新產生 Azure Container Registry 認證
使用 POST HTTP 要求,針對本文中建立的 Azure Container Registry 重新產生其中一個登入認證。
# Variable block
$passwordValue="password"
az rest --method post \
--url https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.ContainerRegistry/registries/$containerRegistryName/regenerateCredential?api-version=2023-01-01-preview \
--body "{'name': '$passwordValue'}"
下列 JSON 字典輸出有省略的欄位,以求簡潔:
{
"passwords": [
{
"name": "password",
"value": "<passwordValue>"
},
{
"name": "password2",
"value": "<passwordValue2>"
}
],
"username": "<containerRegistryName>"
}
要求完成之後,您的指定 Azure Container Registry 認證會連同您現有的密碼 (password2) 一起重新產生新的密碼。
使用 DELETE 刪除您的 Azure Container Registry
使用 DELETE HTTP 要求來刪除現有的 Azure Container Registry。
az rest --method delete \
--url https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroup/providers/Microsoft.ContainerRegistry/registries/$containerRegistryName?api-version=2023-01-01-preview
Microsoft Graph 的其他 az rest 範例
有時候,它有助於查看不同案例的範例,因此以下是使用 Microsoft Graph API 的範例。 若要更新 應用程式的重新導向 URI,請呼叫 更新應用程式 REST API,如下列程式代碼所示:
# Get the application
az rest --method GET \
--uri 'https://graph.microsoft.com/v1.0/applications/b4e4d2ab-e2cb-45d5-a31a-98eb3f364001'
# Update `redirectUris` for `web` property
az rest --method PATCH \
--uri 'https://graph.microsoft.com/v1.0/applications/b4e4d2ab-e2cb-45d5-a31a-98eb3f364001' \
--body '{"web":{"redirectUris":["https://myapp.com"]}}'
清理資源
當您完成本文中建立的資源時,您可以刪除資源群組。 當您刪除資源群組時,會刪除該資源群組中的所有資源。
az group delete --resource-group <resourceGroupName>