在Pulumi Azure Native中获取storage_account - accesskey



我正在尝试从"classic"Azure到Azure Native的Pulumi。我的需求之一是检索Connectionstring和AccessKey到我新创建的StorageAccount。

经典Azure我收到这些字段的

var connectionString=ClassicStorageAccount.PrimaryConnectionString;
var accessKey=ClassicStorageAccount.PrimaryAccessKey;

其中ClassicStorageAccount的类型为Pulumi.Azure.Storage.Account

使用Azure Native创建存储帐户后:

var account=new Pulumi.AzureNative.Storage.StorageAccount("myMagicAccount", new StorageAccountArgs{...});

我正在努力检索AccessKey。

我能够使用

检索connectionstring
var connectionstring=account.StorageAccount.PrimaryEndpoints.Apply(q=>q.Web);

PrimaryEndpointsPrivateEndpointConnections的属性似乎都没有包含我所需的AccessKey。

Azure Native上的StorageAccount文档没有帮助我使用这种方法

你可以使用listStorageAccountKeys方法。

using System.Threading.Tasks;
using Pulumi;
using Pulumi.AzureNative.Resources;
using Pulumi.AzureNative.Storage;
using Pulumi.AzureNative.Storage.Inputs;
class MyStack : Stack
{
public MyStack()
{
// Create an Azure Resource Group
var resourceGroup = new ResourceGroup("resourceGroup");
// Create an Azure resource (Storage Account)
var storageAccount = new StorageAccount("sa", new StorageAccountArgs
{
ResourceGroupName = resourceGroup.Name,
Sku = new SkuArgs
{
Name = SkuName.Standard_LRS
},
Kind = Kind.StorageV2
});
// Export the primary key of the Storage Account
this.PrimaryStorageKey = Output.Tuple(resourceGroup.Name, storageAccount.Name).Apply(names =>
Output.CreateSecret(GetStorageAccountPrimaryKey(names.Item1, names.Item2)));
}
[Output]
public Output<string> PrimaryStorageKey { get; set; }
private static async Task<string> GetStorageAccountPrimaryKey(string resourceGroupName, string accountName)
{
var accountKeys = await ListStorageAccountKeys.InvokeAsync(new ListStorageAccountKeysArgs
{
ResourceGroupName = resourceGroupName,
AccountName = accountName
});
return accountKeys.Keys[0].Value;
}
}

上面的代码来自Pulumi运行pulumi new azure-csharp时使用的模板,可以在模板存储库

中找到。

最新更新