从 C# 创建 Azure 生命周期管理规则
Creating Azure Lifecycle Management rules from C#
有没有办法直接从 C# 为 Azure 存储帐户创建生命周期管理规则?
我知道如何在 Azure 门户或 Terraform 上执行此操作,并且我已经看到 the same question for Java,答案是直接调用 API。如果没有别的办法,我会恢复到这种方法,但我很感兴趣是否有针对此问题的“开箱即用”解决方案,很可能来自某些 Azure 包。
根据@GauravMantri 的回答,我使用了 Microsoft.Azure.Management.Storage 包并使用以下代码创建了管理策略:
var credential = new DefaultAzureCredential();
var token = await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://management.azure.com/.default" }));
var storageClient = new StorageManagementClient(new TokenCredentials(token.Token))
{
SubscriptionId = "InsertSubscriptionId"
};
// Defining the lifecycle management policy
var baseBlob = new ManagementPolicyBaseBlob(delete: new DateAfterModification() { DaysAfterModificationGreaterThan = 365 });
var action = new ManagementPolicyAction(baseBlob);
var filter = new ManagementPolicyFilter(new List<string> { "blockBlob" }, new List<string> { "test/" });
var definition = new ManagementPolicyDefinition(action, filter);
var rule = new ManagementPolicyRule(lifecycleManagementRuleName, definition);
var schema = new ManagementPolicySchema(new List<ManagementPolicyRule> { rule });
// Creating policy
await storageClient.ManagementPolicies.CreateOrUpdateWithHttpMessagesAsync("InsertResourceGroup", options.StorageAccount.Name, schema);
上面定义的规则会在上次修改 365 天后删除 test
容器中的 blob。
有没有办法直接从 C# 为 Azure 存储帐户创建生命周期管理规则?
我知道如何在 Azure 门户或 Terraform 上执行此操作,并且我已经看到 the same question for Java,答案是直接调用 API。如果没有别的办法,我会恢复到这种方法,但我很感兴趣是否有针对此问题的“开箱即用”解决方案,很可能来自某些 Azure 包。
根据@GauravMantri 的回答,我使用了 Microsoft.Azure.Management.Storage 包并使用以下代码创建了管理策略:
var credential = new DefaultAzureCredential();
var token = await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://management.azure.com/.default" }));
var storageClient = new StorageManagementClient(new TokenCredentials(token.Token))
{
SubscriptionId = "InsertSubscriptionId"
};
// Defining the lifecycle management policy
var baseBlob = new ManagementPolicyBaseBlob(delete: new DateAfterModification() { DaysAfterModificationGreaterThan = 365 });
var action = new ManagementPolicyAction(baseBlob);
var filter = new ManagementPolicyFilter(new List<string> { "blockBlob" }, new List<string> { "test/" });
var definition = new ManagementPolicyDefinition(action, filter);
var rule = new ManagementPolicyRule(lifecycleManagementRuleName, definition);
var schema = new ManagementPolicySchema(new List<ManagementPolicyRule> { rule });
// Creating policy
await storageClient.ManagementPolicies.CreateOrUpdateWithHttpMessagesAsync("InsertResourceGroup", options.StorageAccount.Name, schema);
上面定义的规则会在上次修改 365 天后删除 test
容器中的 blob。