如何使用REST API或Python库启用Azure存储账户Blob/容器软删除
使用API启用Azure Blob存储软删除及配置保留策略
通过REST API配置
调用Set Blob Service Properties API即可启用Blob软删除并设置保留策略:
- 请求方法:PUT
- 请求地址:
https://{storage-account-name}.blob.core.windows.net/?restype=service&comp=properties - 请求头:需包含授权信息(SharedKey或Azure AD令牌)、
Content-Type: application/xml
请求体示例(XML格式):
<?xml version="1.0" encoding="utf-8"?> <StorageServiceProperties> <DeleteRetentionPolicy> <Enabled>true</Enabled> <Days>30</Days> <!-- 替换为你需要的保留天数,对应图中设置 --> </DeleteRetentionPolicy> </StorageServiceProperties>
使用.NET SDK配置
借助Azure.Storage.Blobs SDK可以更便捷地完成配置:
- 安装NuGet包:
Azure.Storage.Blobs - 示例代码:
using Azure.Storage.Blobs; using Azure.Storage.Blobs.Models; var blobServiceClient = new BlobServiceClient("<你的存储账户连接字符串>"); var serviceProperties = await blobServiceClient.GetPropertiesAsync(); // 启用软删除并设置保留天数 serviceProperties.DeleteRetentionPolicy.Enabled = true; serviceProperties.DeleteRetentionPolicy.Days = 30; await blobServiceClient.SetPropertiesAsync(serviceProperties);
验证配置
调用Get Blob Service Properties API(GET方法,地址同配置API),返回结果中查看DeleteRetentionPolicy节点的Enabled和Days值,确认配置生效。
内容的提问来源于stack exchange,提问作者Eli m
相关产品推荐
相关产品推荐

