使用不允许Blob公共访问的Fluent API在C#中创建Azure存储帐户

使用不允许Blob公共访问的Fluent API在C#中创建Azure存储帐户,c#,azure,azure-storage,azure-storage-blobs,C#,Azure,Azure Storage,Azure Storage Blobs,我的目标是使用Fluent API(Microsoft.Azure.Management.Fluent)从C#code创建一个Azure存储帐户 我的代码执行正确,但我的组织有一个策略,该策略要求创建所有存储帐户时必须将“允许Blob公共访问”设置为禁用。有没有办法使用Fluent方法将其指定为选项 TargetStorageAccount = AzureClient.StorageAccounts.Define(storageAccountName)

我的目标是使用Fluent API(Microsoft.Azure.Management.Fluent)从C#code创建一个Azure存储帐户

我的代码执行正确,但我的组织有一个策略,该策略要求创建所有存储帐户时必须将“允许Blob公共访问”设置为禁用。有没有办法使用Fluent方法将其指定为选项

                TargetStorageAccount = AzureClient.StorageAccounts.Define(storageAccountName)
                    .WithRegion(Region.AustraliaCentral)
                    .WithExistingResourceGroup(ResourceGroupName)
                    .WithSku(StorageAccountSkuType.Standard_LRS)
                    .WithGeneralPurposeAccountKindV2()
                    .Create();

您可以使用ARM模板部署“允许Blob公共访问”设置为禁用的存储帐户

为了快速演示,这是我的存储帐户模板:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storagePrefix": {
            "type": "string",
            "minLength": 3,
            "maxLength": 11
        },
        "storageSKU": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_RAGRS",
                "Standard_ZRS",
                "Premium_LRS",
                "Premium_ZRS",
                "Standard_GZRS",
                "Standard_RAGZRS"
            ]
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]"
        }
    },

    "variables": {
        "uniqueStorageName": "[concat(parameters('storagePrefix'), '4testonly')]"
    },
    "resources": [{
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[variables('uniqueStorageName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "[parameters('storageSKU')]"
            },
            "kind": "StorageV2",
            "properties": {
                "supportsHttpsTrafficOnly": true,
                "allowBlobPublicAccess": false
            }
        }
    ],
    "outputs": {
        "result": {
            "type": "string",
            "value": "done"
        }
    }
}
allowBlobPublicAccess设置为false

这是使用fluent API创建此部署的代码:

    var storageAccountName = "<your new storage account name>";

    var templateJson = File.ReadAllText(@"<path of template file>\storageOnly.json");

    azure.Deployments.Define("storageDepolyTest")
            .WithExistingResourceGroup("<your resource group name>")
            .WithTemplate(templateJson)
            .WithParameters("{\"storagePrefix\":{\"value\":\""+ storageAccountName + "\"}}")
            .WithMode(DeploymentMode.Incremental)
            .Create();
var-storageAccountName=“”;
var templateJson=File.ReadAllText(@“\storageOnly.json”);
azure.Deployments.Define(“storageDepolyTest”)
.使用现有资源组(“”)
.WithTemplate(templateJson)
.WithParameters(“{\'storagePrefix\”:{\'value\:\”+storageAccountName+“\”}}”)
.WithMode(DeploymentMode.Incremental)
.Create();
结果:


您可以使用ARM模板部署“允许Blob公共访问”设置为禁用的存储帐户

为了快速演示,这是我的存储帐户模板:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storagePrefix": {
            "type": "string",
            "minLength": 3,
            "maxLength": 11
        },
        "storageSKU": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_RAGRS",
                "Standard_ZRS",
                "Premium_LRS",
                "Premium_ZRS",
                "Standard_GZRS",
                "Standard_RAGZRS"
            ]
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]"
        }
    },

    "variables": {
        "uniqueStorageName": "[concat(parameters('storagePrefix'), '4testonly')]"
    },
    "resources": [{
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[variables('uniqueStorageName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "[parameters('storageSKU')]"
            },
            "kind": "StorageV2",
            "properties": {
                "supportsHttpsTrafficOnly": true,
                "allowBlobPublicAccess": false
            }
        }
    ],
    "outputs": {
        "result": {
            "type": "string",
            "value": "done"
        }
    }
}
allowBlobPublicAccess设置为false

这是使用fluent API创建此部署的代码:

    var storageAccountName = "<your new storage account name>";

    var templateJson = File.ReadAllText(@"<path of template file>\storageOnly.json");

    azure.Deployments.Define("storageDepolyTest")
            .WithExistingResourceGroup("<your resource group name>")
            .WithTemplate(templateJson)
            .WithParameters("{\"storagePrefix\":{\"value\":\""+ storageAccountName + "\"}}")
            .WithMode(DeploymentMode.Incremental)
            .Create();
var-storageAccountName=“”;
var templateJson=File.ReadAllText(@“\storageOnly.json”);
azure.Deployments.Define(“storageDepolyTest”)
.使用现有资源组(“”)
.WithTemplate(templateJson)
.WithParameters(“{\'storagePrefix\”:{\'value\:\”+storageAccountName+“\”}}”)
.WithMode(DeploymentMode.Incremental)
.Create();
结果:


我认为目前Fluent没有选项可用于创建禁用“允许blob公共访问”的存储帐户。目前,使用RESTAPI可能是您的最佳选择。进展如何?你的问题解决了吗?没有,显然fluent API无法达到目标。因此,我又回到了使用Microsoft.Azure.Management.Storage.StorageManagementClient以编程方式创建禁止创建公共容器的存储帐户。这太糟糕了,因为我宁愿使用fluent API,但它不完整。@Bonnie我已经更新了我的答案。您可以使用ARM模板和fluent API来实现这一点。我认为fluent目前没有选项可以用于创建禁用“允许blob公共访问”的存储帐户。目前,使用RESTAPI可能是您的最佳选择。进展如何?你的问题解决了吗?没有,显然fluent API无法达到目标。因此,我又回到了使用Microsoft.Azure.Management.Storage.StorageManagementClient以编程方式创建禁止创建公共容器的存储帐户。这太糟糕了,因为我宁愿使用fluent API,但它不完整。@Bonnie我已经更新了我的答案。你可以使用ARM模板和fluent API来实现这一点,我不确定你是否理解这个问题。我首先不能以编程方式创建存储帐户,因为有一个策略阻止创建允许公共容器的存储帐户。因此,我无法通过编程方式创建存储帐户,然后通过fluent API将其更新为不允许使用公共容器。@Bonnie,感谢您提供的提示,实际上您可以使用fluent API来做到这一点,我已更新了我的答案。我感谢您为此所做的所有工作。我想我想指出的是,使用ARM模板比使用Microsoft.Azure.Management.Storage.StorageManagementClient要复杂得多。@Bonnie,明白了,如果这篇文章有帮助,你能接受吗?因此,它也将帮助其他有类似问题的人解决这个问题。我不确定你是否理解这个问题。我首先不能以编程方式创建存储帐户,因为有一个策略阻止创建允许公共容器的存储帐户。因此,我无法通过编程方式创建存储帐户,然后通过fluent API将其更新为不允许使用公共容器。@Bonnie,感谢您提供的提示,实际上您可以使用fluent API来做到这一点,我已更新了我的答案。我感谢您为此所做的所有工作。我想我想指出的是,使用ARM模板比使用Microsoft.Azure.Management.Storage.StorageManagementClient要复杂得多。@Bonnie,明白了,如果这篇文章有帮助,你能接受吗?这样它也会帮助其他有类似问题的人解决这个问题