Azure REST Api 创建经典部署模型存储帐户
Azure REST Api to create classic deployment model storage account
我正在尝试创建 Azure 存储帐户类型:Storage (classic)
通过 REST APi。
当我发送此请求时:
PUT https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Storage/storageAccounts/{{name}}?api-version=2019-04-01
与 body:
{
"sku": {
"name": "Standard_GRS"
},
"kind": "Storage",
"location": "eastus2"
}
它工作得很好,但我创建的存储空间很好:Storage (general purpose v1)
。
我试过像这样用 Microsoft.ClassicStorage
发送请求:
PUT https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.ClassicStorage/storageAccounts/{{name}}?api-version=2016-11-01
与以前相同 body(也尝试不使用 "kind"
参数),我得到响应:400 Bad Request
{
"error": {
"code": "InvalidStorageAccountRequest",
"message": "The storage account '{{validname}}' request is invalid."
}
}
知道应该在请求 body 中放置什么吗?或者是否可以通过 REST API 或通过 C# 代码创建存储(经典)?
我以前遇到过这种问题,这似乎是因为您为存储帐户提供的名称,但对我来说,无效代码是下面的代码:
代码=账号无效
It is not a valid storage account name. Storage account name must be
between 3 and 24 characters in length and use numbers and lower-case letters only.
您可以在 Azure 文档中查看 link。
请尝试将您的请求正文更改为如下内容:
{
"properties": {
"accountType": "Standard-GRS"
},
"location": "eastus2"
}
经典存储帐户的帐户类型与新存储帐户的帐户类型不同。它们是 Standard-GRS
、Standard-LRS
、Standard-RAGRS
、Standard-ZRS
和 Premium-LRS
。
解决方案 (.NET)
Storage Account (classic)没有"Export template"的选项,但可以在其他地方找到,如下:
- 转到https://portal.azure.com
- 创建资源(存储帐户)
- 点击"Choose classic deployment model"
- 选择标签 "Review + create"
- 点击"Download a template for automation"
您应该得到两个 JSON 文件:
- parameter.json(这里可以更改参数值):
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"value": "westeurope"
},
"storageAccountName": {
"value": "testtemplate"
},
"accountType": {
"value": "Standard_GRS"
}
}
}
- template.json
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"storageAccountName": {
"type": "string"
},
"accountType": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"name": "[parameters('storageAccountName')]",
"type": "Microsoft.ClassicStorage/storageAccounts",
"apiVersion": "2016-11-01",
"location": "[parameters('location')]",
"properties": {
"accountType": "[parameters('accountType')]"
},
"dependsOn": []
}
],
"outputs": {}
}
现在要部署它,您只需要在下面通过它,并提供所有私人凭据
(代码是从 Storage Account kind V2 "Export template" 选项卡中复制的)
// Requires the following Azure NuGet packages and related dependencies:
// package id="Microsoft.Azure.Management.Authorization" version="2.0.0"
// package id="Microsoft.Azure.Management.ResourceManager" version="1.4.0-preview"
// package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.2.8-preview"
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Rest.Azure.Authentication;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
namespace PortalGenerated
{
/// <summary>
/// This is a helper class for deploying an Azure Resource Manager template
/// More info about template deployments can be found here https://go.microsoft.com/fwLink/?LinkID=733371
/// </summary>
class DeploymentHelper
{
string subscriptionId = "your-subscription-id";
string clientId = "your-service-principal-clientId";
string clientSecret = "your-service-principal-client-secret";
string resourceGroupName = "resource-group-name";
string deploymentName = "deployment-name";
string resourceGroupLocation = "resource-group-location"; // must be specified for creating a new resource group
string pathToTemplateFile = "path-to-template.json-on-disk";
string pathToParameterFile = "path-to-parameters.json-on-disk";
string tenantId = "tenant-id";
public async void Run()
{
// Try to obtain the service credentials
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, clientSecret);
// Read the template and parameter file contents
JObject templateFileContents = GetJsonFileContents(pathToTemplateFile);
JObject parameterFileContents = GetJsonFileContents(pathToParameterFile);
// Create the resource manager client
var resourceManagementClient = new ResourceManagementClient(serviceCreds);
resourceManagementClient.SubscriptionId = subscriptionId;
// Create or check that resource group exists
EnsureResourceGroupExists(resourceManagementClient, resourceGroupName, resourceGroupLocation);
// Start a deployment
DeployTemplate(resourceManagementClient, resourceGroupName, deploymentName, templateFileContents, parameterFileContents);
}
/// <summary>
/// Reads a JSON file from the specified path
/// </summary>
/// <param name="pathToJson">The full path to the JSON file</param>
/// <returns>The JSON file contents</returns>
private JObject GetJsonFileContents(string pathToJson)
{
JObject templatefileContent = new JObject();
using (StreamReader file = File.OpenText(pathToJson))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
templatefileContent = (JObject)JToken.ReadFrom(reader);
return templatefileContent;
}
}
}
/// <summary>
/// Ensures that a resource group with the specified name exists. If it does not, will attempt to create one.
/// </summary>
/// <param name="resourceManagementClient">The resource manager client.</param>
/// <param name="resourceGroupName">The name of the resource group.</param>
/// <param name="resourceGroupLocation">The resource group location. Required when creating a new resource group.</param>
private static void EnsureResourceGroupExists(ResourceManagementClient resourceManagementClient, string resourceGroupName, string resourceGroupLocation)
{
if (resourceManagementClient.ResourceGroups.CheckExistence(resourceGroupName) != true)
{
Console.WriteLine(string.Format("Creating resource group '{0}' in location '{1}'", resourceGroupName, resourceGroupLocation));
var resourceGroup = new ResourceGroup();
resourceGroup.Location = resourceGroupLocation;
resourceManagementClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);
}
else
{
Console.WriteLine(string.Format("Using existing resource group '{0}'", resourceGroupName));
}
}
/// <summary>
/// Starts a template deployment.
/// </summary>
/// <param name="resourceManagementClient">The resource manager client.</param>
/// <param name="resourceGroupName">The name of the resource group.</param>
/// <param name="deploymentName">The name of the deployment.</param>
/// <param name="templateFileContents">The template file contents.</param>
/// <param name="parameterFileContents">The parameter file contents.</param>
private static void DeployTemplate(ResourceManagementClient resourceManagementClient, string resourceGroupName, string deploymentName, JObject templateFileContents, JObject parameterFileContents)
{
Console.WriteLine(string.Format("Starting template deployment '{0}' in resource group '{1}'", deploymentName, resourceGroupName));
var deployment = new Deployment();
deployment.Properties = new DeploymentProperties
{
Mode = DeploymentMode.Incremental,
Template = templateFileContents,
Parameters = parameterFileContents["parameters"].ToObject<JObject>()
};
var deploymentResult = resourceManagementClient.Deployments.CreateOrUpdate(resourceGroupName, deploymentName, deployment);
Console.WriteLine(string.Format("Deployment status: {0}", deploymentResult.Properties.ProvisioningState));
}
}
}
我正在尝试创建 Azure 存储帐户类型:Storage (classic)
通过 REST APi。
当我发送此请求时:
PUT https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Storage/storageAccounts/{{name}}?api-version=2019-04-01
与 body:
{
"sku": {
"name": "Standard_GRS"
},
"kind": "Storage",
"location": "eastus2"
}
它工作得很好,但我创建的存储空间很好:Storage (general purpose v1)
。
我试过像这样用 Microsoft.ClassicStorage
发送请求:
PUT https://management.azure.com/subscriptions/{{subscriptionId}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.ClassicStorage/storageAccounts/{{name}}?api-version=2016-11-01
与以前相同 body(也尝试不使用 "kind"
参数),我得到响应:400 Bad Request
{
"error": {
"code": "InvalidStorageAccountRequest",
"message": "The storage account '{{validname}}' request is invalid."
}
}
知道应该在请求 body 中放置什么吗?或者是否可以通过 REST API 或通过 C# 代码创建存储(经典)?
我以前遇到过这种问题,这似乎是因为您为存储帐户提供的名称,但对我来说,无效代码是下面的代码:
代码=账号无效
It is not a valid storage account name. Storage account name must be between 3 and 24 characters in length and use numbers and lower-case letters only.
您可以在 Azure 文档中查看 link。
请尝试将您的请求正文更改为如下内容:
{
"properties": {
"accountType": "Standard-GRS"
},
"location": "eastus2"
}
经典存储帐户的帐户类型与新存储帐户的帐户类型不同。它们是 Standard-GRS
、Standard-LRS
、Standard-RAGRS
、Standard-ZRS
和 Premium-LRS
。
解决方案 (.NET)
Storage Account (classic)没有"Export template"的选项,但可以在其他地方找到,如下:
- 转到https://portal.azure.com
- 创建资源(存储帐户)
- 点击"Choose classic deployment model"
- 选择标签 "Review + create"
- 点击"Download a template for automation"
您应该得到两个 JSON 文件:
- parameter.json(这里可以更改参数值):
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"value": "westeurope"
},
"storageAccountName": {
"value": "testtemplate"
},
"accountType": {
"value": "Standard_GRS"
}
}
}
- template.json
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"storageAccountName": {
"type": "string"
},
"accountType": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"name": "[parameters('storageAccountName')]",
"type": "Microsoft.ClassicStorage/storageAccounts",
"apiVersion": "2016-11-01",
"location": "[parameters('location')]",
"properties": {
"accountType": "[parameters('accountType')]"
},
"dependsOn": []
}
],
"outputs": {}
}
现在要部署它,您只需要在下面通过它,并提供所有私人凭据 (代码是从 Storage Account kind V2 "Export template" 选项卡中复制的)
// Requires the following Azure NuGet packages and related dependencies:
// package id="Microsoft.Azure.Management.Authorization" version="2.0.0"
// package id="Microsoft.Azure.Management.ResourceManager" version="1.4.0-preview"
// package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.2.8-preview"
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Rest.Azure.Authentication;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
namespace PortalGenerated
{
/// <summary>
/// This is a helper class for deploying an Azure Resource Manager template
/// More info about template deployments can be found here https://go.microsoft.com/fwLink/?LinkID=733371
/// </summary>
class DeploymentHelper
{
string subscriptionId = "your-subscription-id";
string clientId = "your-service-principal-clientId";
string clientSecret = "your-service-principal-client-secret";
string resourceGroupName = "resource-group-name";
string deploymentName = "deployment-name";
string resourceGroupLocation = "resource-group-location"; // must be specified for creating a new resource group
string pathToTemplateFile = "path-to-template.json-on-disk";
string pathToParameterFile = "path-to-parameters.json-on-disk";
string tenantId = "tenant-id";
public async void Run()
{
// Try to obtain the service credentials
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, clientSecret);
// Read the template and parameter file contents
JObject templateFileContents = GetJsonFileContents(pathToTemplateFile);
JObject parameterFileContents = GetJsonFileContents(pathToParameterFile);
// Create the resource manager client
var resourceManagementClient = new ResourceManagementClient(serviceCreds);
resourceManagementClient.SubscriptionId = subscriptionId;
// Create or check that resource group exists
EnsureResourceGroupExists(resourceManagementClient, resourceGroupName, resourceGroupLocation);
// Start a deployment
DeployTemplate(resourceManagementClient, resourceGroupName, deploymentName, templateFileContents, parameterFileContents);
}
/// <summary>
/// Reads a JSON file from the specified path
/// </summary>
/// <param name="pathToJson">The full path to the JSON file</param>
/// <returns>The JSON file contents</returns>
private JObject GetJsonFileContents(string pathToJson)
{
JObject templatefileContent = new JObject();
using (StreamReader file = File.OpenText(pathToJson))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
templatefileContent = (JObject)JToken.ReadFrom(reader);
return templatefileContent;
}
}
}
/// <summary>
/// Ensures that a resource group with the specified name exists. If it does not, will attempt to create one.
/// </summary>
/// <param name="resourceManagementClient">The resource manager client.</param>
/// <param name="resourceGroupName">The name of the resource group.</param>
/// <param name="resourceGroupLocation">The resource group location. Required when creating a new resource group.</param>
private static void EnsureResourceGroupExists(ResourceManagementClient resourceManagementClient, string resourceGroupName, string resourceGroupLocation)
{
if (resourceManagementClient.ResourceGroups.CheckExistence(resourceGroupName) != true)
{
Console.WriteLine(string.Format("Creating resource group '{0}' in location '{1}'", resourceGroupName, resourceGroupLocation));
var resourceGroup = new ResourceGroup();
resourceGroup.Location = resourceGroupLocation;
resourceManagementClient.ResourceGroups.CreateOrUpdate(resourceGroupName, resourceGroup);
}
else
{
Console.WriteLine(string.Format("Using existing resource group '{0}'", resourceGroupName));
}
}
/// <summary>
/// Starts a template deployment.
/// </summary>
/// <param name="resourceManagementClient">The resource manager client.</param>
/// <param name="resourceGroupName">The name of the resource group.</param>
/// <param name="deploymentName">The name of the deployment.</param>
/// <param name="templateFileContents">The template file contents.</param>
/// <param name="parameterFileContents">The parameter file contents.</param>
private static void DeployTemplate(ResourceManagementClient resourceManagementClient, string resourceGroupName, string deploymentName, JObject templateFileContents, JObject parameterFileContents)
{
Console.WriteLine(string.Format("Starting template deployment '{0}' in resource group '{1}'", deploymentName, resourceGroupName));
var deployment = new Deployment();
deployment.Properties = new DeploymentProperties
{
Mode = DeploymentMode.Incremental,
Template = templateFileContents,
Parameters = parameterFileContents["parameters"].ToObject<JObject>()
};
var deploymentResult = resourceManagementClient.Deployments.CreateOrUpdate(resourceGroupName, deploymentName, deployment);
Console.WriteLine(string.Format("Deployment status: {0}", deploymentResult.Properties.ProvisioningState));
}
}
}