Microsoft Azure Storage Management Client Library for Python
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Lifecycle management policies, object replication policies, and blob inventory policies for automated data management. These policies enable automated data lifecycle management, cross-region replication, and comprehensive data inventory reporting.
Configure automated lifecycle management rules to transition blobs between access tiers and delete expired data.
class ManagementPoliciesOperations:
def create_or_update(
self,
resource_group_name: str,
account_name: str,
management_policy_name: ManagementPolicyName,
properties: ManagementPolicy
) -> ManagementPolicy:
"""
Sets the managementpolicy to the specified storage account.
Parameters:
- resource_group_name: Name of the resource group
- account_name: Name of the storage account
- management_policy_name: Name of the management policy (always "default")
- properties: Management policy configuration and rules
Returns:
Created or updated ManagementPolicy
"""
def get(
self,
resource_group_name: str,
account_name: str,
management_policy_name: ManagementPolicyName
) -> ManagementPolicy:
"""
Gets the managementpolicy associated with the specified storage account.
Parameters:
- resource_group_name: Name of the resource group
- account_name: Name of the storage account
- management_policy_name: Name of the management policy
Returns:
Current ManagementPolicy configuration
"""
def delete(
self,
resource_group_name: str,
account_name: str,
management_policy_name: ManagementPolicyName
) -> None:
"""
Deletes the managementpolicy associated with the specified storage account.
Parameters:
- resource_group_name: Name of the resource group
- account_name: Name of the storage account
- management_policy_name: Name of the management policy to delete
"""Usage example:
from azure.mgmt.storage.models import (
ManagementPolicy, ManagementPolicySchema, ManagementPolicyRule,
ManagementPolicyDefinition, ManagementPolicyAction, ManagementPolicyFilter,
ManagementPolicyBaseBlob, ManagementPolicySnapShot, ManagementPolicyVersion,
DateAfterModification, DateAfterCreation, ManagementPolicyName
)
# Create lifecycle management policy with multiple rules
# Rule 1: Transition hot blobs to cool after 30 days, archive after 90 days
hot_to_cool_action = ManagementPolicyBaseBlob(
tier_to_cool=DateAfterModification(days_after_modification_greater_than=30),
tier_to_archive=DateAfterModification(days_after_modification_greater_than=90),
delete=DateAfterModification(days_after_modification_greater_than=2555) # 7 years
)
# Rule 2: Delete old snapshots after 30 days
snapshot_action = ManagementPolicySnapShot(
delete=DateAfterCreation(days_after_creation_greater_than=30)
)
# Rule 3: Delete old versions after 365 days
version_action = ManagementPolicyVersion(
delete=DateAfterCreation(days_after_creation_greater_than=365)
)
# Combine actions
lifecycle_action = ManagementPolicyAction(
base_blob=hot_to_cool_action,
snapshot=snapshot_action,
version=version_action
)
# Create filter for specific blob types and prefixes
blob_filter = ManagementPolicyFilter(
prefix_match=["logs/", "temp/", "backup/"],
blob_types=["blockBlob"],
blob_index_match=[
TagFilter(name="Department", op="==", value="Finance"),
TagFilter(name="DataType", op="==", value="Archive")
]
)
# Define the rule
lifecycle_rule = ManagementPolicyRule(
enabled=True,
name="DataLifecycleRule",
type_="Lifecycle",
definition=ManagementPolicyDefinition(
actions=lifecycle_action,
filters=blob_filter
)
)
# Create the management policy
management_policy = ManagementPolicy(
properties=ManagementPolicyProperties(
policy=ManagementPolicySchema(
rules=[lifecycle_rule]
)
)
)
created_policy = client.management_policies.create_or_update(
resource_group_name="my-resource-group",
account_name="mystorageaccount123",
management_policy_name=ManagementPolicyName.DEFAULT,
properties=management_policy
)
print(f"Created lifecycle policy with {len(created_policy.properties.policy.rules)} rules")
# Create a more complex policy with multiple rules
rules = []
# Rule for log files - delete after 90 days
log_rule = ManagementPolicyRule(
enabled=True,
name="LogRetentionRule",
type_="Lifecycle",
definition=ManagementPolicyDefinition(
actions=ManagementPolicyAction(
base_blob=ManagementPolicyBaseBlob(
delete=DateAfterModification(days_after_modification_greater_than=90)
)
),
filters=ManagementPolicyFilter(
prefix_match=["logs/", "diagnostics/"],
blob_types=["blockBlob", "appendBlob"]
)
)
)
# Rule for backup files - move to archive after 30 days, delete after 7 years
backup_rule = ManagementPolicyRule(
enabled=True,
name="BackupArchivalRule",
type_="Lifecycle",
definition=ManagementPolicyDefinition(
actions=ManagementPolicyAction(
base_blob=ManagementPolicyBaseBlob(
tier_to_archive=DateAfterModification(days_after_modification_greater_than=30),
delete=DateAfterModification(days_after_modification_greater_than=2555)
)
),
filters=ManagementPolicyFilter(
prefix_match=["backups/"],
blob_types=["blockBlob"]
)
)
)
# Rule for temporary files - delete after 7 days
temp_rule = ManagementPolicyRule(
enabled=True,
name="TempFileCleanup",
type_="Lifecycle",
definition=ManagementPolicyDefinition(
actions=ManagementPolicyAction(
base_blob=ManagementPolicyBaseBlob(
delete=DateAfterModification(days_after_modification_greater_than=7)
)
),
filters=ManagementPolicyFilter(
prefix_match=["temp/", "tmp/", "staging/"],
blob_types=["blockBlob"]
)
)
)
rules.extend([log_rule, backup_rule, temp_rule])
# Create comprehensive policy
comprehensive_policy = ManagementPolicy(
properties=ManagementPolicyProperties(
policy=ManagementPolicySchema(rules=rules)
)
)
client.management_policies.create_or_update(
resource_group_name="my-resource-group",
account_name="mystorageaccount123",
management_policy_name=ManagementPolicyName.DEFAULT,
properties=comprehensive_policy
)Configure cross-region object replication for disaster recovery and data distribution.
class ObjectReplicationPoliciesOperations:
def create_or_update(
self,
resource_group_name: str,
account_name: str,
object_replication_policy_id: str,
properties: ObjectReplicationPolicy
) -> ObjectReplicationPolicy:
"""
Creates or updates the object replication policy of the storage account.
Parameters:
- resource_group_name: Name of the resource group
- account_name: Name of the storage account
- object_replication_policy_id: ID of the object replication policy
- properties: Object replication policy configuration
Returns:
Created or updated ObjectReplicationPolicy
"""
def get(
self,
resource_group_name: str,
account_name: str,
object_replication_policy_id: str
) -> ObjectReplicationPolicy:
"""
Gets the object replication policy of the storage account.
Parameters:
- resource_group_name: Name of the resource group
- account_name: Name of the storage account
- object_replication_policy_id: ID of the object replication policy
Returns:
ObjectReplicationPolicy configuration
"""
def delete(
self,
resource_group_name: str,
account_name: str,
object_replication_policy_id: str
) -> None:
"""
Deletes the object replication policy of the storage account.
Parameters:
- resource_group_name: Name of the resource group
- account_name: Name of the storage account
- object_replication_policy_id: ID of the object replication policy
"""
def list(
self,
resource_group_name: str,
account_name: str
) -> ItemPaged[ObjectReplicationPolicy]:
"""
List the object replication policies associated with the storage account.
Parameters:
- resource_group_name: Name of the resource group
- account_name: Name of the storage account
Returns:
Paginated list of ObjectReplicationPolicy objects
"""Usage example:
from azure.mgmt.storage.models import (
ObjectReplicationPolicy, ObjectReplicationPolicyRule,
ObjectReplicationPolicyFilter
)
# Create object replication policy for disaster recovery
# Source: East US storage account
# Destination: West US storage account
replication_filter = ObjectReplicationPolicyFilter(
prefix_match=["critical-data/", "customer-data/"],
min_creation_time="2024-01-01T00:00:00Z"
)
replication_rule = ObjectReplicationPolicyRule(
rule_id="rule1",
source_container="production-data",
destination_container="replicated-data",
filters=replication_filter
)
replication_policy = ObjectReplicationPolicy(
properties=ObjectReplicationPolicyProperties(
source_account="/subscriptions/sub-id/resourceGroups/my-rg/providers/Microsoft.Storage/storageAccounts/sourcestorage",
destination_account="/subscriptions/sub-id/resourceGroups/my-rg-west/providers/Microsoft.Storage/storageAccounts/deststorage",
rules=[replication_rule]
)
)
created_replication = client.object_replication_policies.create_or_update(
resource_group_name="my-resource-group",
account_name="sourcestorage",
object_replication_policy_id="policy1",
properties=replication_policy
)
print(f"Created replication policy: {created_replication.name}")
# Create multi-container replication policy
multi_rules = [
ObjectReplicationPolicyRule(
rule_id="documents-rule",
source_container="documents",
destination_container="documents-replica",
filters=ObjectReplicationPolicyFilter(
prefix_match=["important/", "contracts/"]
)
),
ObjectReplicationPolicyRule(
rule_id="images-rule",
source_container="images",
destination_container="images-replica",
filters=ObjectReplicationPolicyFilter(
prefix_match=["profile-pics/", "product-images/"]
)
),
ObjectReplicationPolicyRule(
rule_id="backups-rule",
source_container="backups",
destination_container="backups-replica"
# No filter - replicate all backup data
)
]
multi_replication_policy = ObjectReplicationPolicy(
properties=ObjectReplicationPolicyProperties(
source_account="/subscriptions/sub-id/resourceGroups/my-rg/providers/Microsoft.Storage/storageAccounts/sourcestorage",
destination_account="/subscriptions/sub-id/resourceGroups/my-rg-west/providers/Microsoft.Storage/storageAccounts/deststorage",
rules=multi_rules
)
)
client.object_replication_policies.create_or_update(
resource_group_name="my-resource-group",
account_name="sourcestorage",
object_replication_policy_id="multi-container-policy",
properties=multi_replication_policy
)
# List all replication policies
replication_policies = list(client.object_replication_policies.list(
resource_group_name="my-resource-group",
account_name="sourcestorage"
))
print(f"Total replication policies: {len(replication_policies)}")
for policy in replication_policies:
print(f"Policy: {policy.name}")
print(f" Rules: {len(policy.properties.rules)}")
print(f" Destination: {policy.properties.destination_account}")class ManagementPolicy:
"""Lifecycle management policy resource."""
id: str
name: str
type_: str
properties: ManagementPolicyProperties
class ManagementPolicyProperties:
"""Properties of management policy."""
policy: ManagementPolicySchema
last_modified_time: datetime
class ManagementPolicySchema:
"""Schema for management policy."""
rules: List[ManagementPolicyRule]
class ManagementPolicyRule:
"""Individual rule in management policy."""
enabled: bool
name: str
type_: str
definition: ManagementPolicyDefinition
class ManagementPolicyDefinition:
"""Definition of management policy rule."""
actions: ManagementPolicyAction
filters: ManagementPolicyFilter
class ManagementPolicyAction:
"""Actions to perform on matching blobs."""
base_blob: ManagementPolicyBaseBlob
snapshot: ManagementPolicySnapShot
version: ManagementPolicyVersion
class ManagementPolicyBaseBlob:
"""Actions for base blobs."""
tier_to_cool: DateAfterModification
tier_to_archive: DateAfterModification
tier_to_cold: DateAfterModification
delete: DateAfterModification
enable_auto_tier_to_hot_from_cool: bool
class ManagementPolicySnapShot:
"""Actions for blob snapshots."""
tier_to_cool: DateAfterCreation
tier_to_archive: DateAfterCreation
tier_to_cold: DateAfterCreation
delete: DateAfterCreation
class ManagementPolicyVersion:
"""Actions for blob versions."""
tier_to_cool: DateAfterCreation
tier_to_archive: DateAfterCreation
tier_to_cold: DateAfterCreation
delete: DateAfterCreation
class ManagementPolicyFilter:
"""Filter criteria for management policy rules."""
prefix_match: List[str]
blob_types: List[str]
blob_index_match: List[TagFilter]
class DateAfterModification:
"""Date condition based on modification time."""
days_after_modification_greater_than: float
days_after_last_access_time_greater_than: float
class DateAfterCreation:
"""Date condition based on creation time."""
days_after_creation_greater_than: float
days_after_last_tier_change_greater_than: float
class ObjectReplicationPolicy:
"""Object replication policy resource."""
id: str
name: str
type_: str
properties: ObjectReplicationPolicyProperties
class ObjectReplicationPolicyProperties:
"""Properties of object replication policy."""
policy_id: str
enabled_time: datetime
source_account: str
destination_account: str
rules: List[ObjectReplicationPolicyRule]
class ObjectReplicationPolicyRule:
"""Rule for object replication."""
rule_id: str
source_container: str
destination_container: str
filters: ObjectReplicationPolicyFilter
class ObjectReplicationPolicyFilter:
"""Filter for object replication."""
prefix_match: List[str]
min_creation_time: str
class TagFilter:
"""Tag-based filter condition."""
name: str
op: str
value: str
class ManagementPolicyName(str, Enum):
"""Management policy names."""
DEFAULT = "default"Install with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-storage