删除 Blob 存储中的 Blob,其数量超过天数
以下是 Azure Powershell 自动化 Runbook 的示例,该 Runbook 删除 Azure 存储容器中超过几天的任何 Blob。
这对于删除旧的 SQL 备份以节省成本和空间非常有用。
它需要许多自我解释的参数。
注意:我留下了一些注释掉的代码来帮助调试
它使用 Azure 在你创建自动化帐户时可以自动为你设置的服务主体。你需要具有 Azure Active Directory 访问权限。见图:
<#
.DESCRIPTION
Removes all blobs older than a number of days back using the Run As Account (Service Principal)
.NOTES
AUTHOR: Russ
LASTEDIT: Oct 03, 2016 #>
param(
[parameter(Mandatory=$true)]
[String]$resourceGroupName,
[parameter(Mandatory=$true)]
[String]$connectionName,
# StorageAccount name for content deletion.
[Parameter(Mandatory = $true)]
[String]$StorageAccountName,
# StorageContainer name for content deletion.
[Parameter(Mandatory = $true)]
[String]$ContainerName,
[Parameter(Mandatory = $true)]
[Int32]$DaysOld
)
$VerbosePreference = "Continue";
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
$keys = Get-AzureRMStorageAccountKey -ResourceGroupName $resourceGroupName -AccountName $StorageAccountName
# get the storage account key
Write-Host "The storage key is: "$StorageAccountKey;
# get the context
$StorageAccountContext = New-AzureStorageContext -storageAccountName $StorageAccountName -StorageAccountKey $keys.Key1 #.Value;
$StorageAccountContext;
$existingContainer = Get-AzureStorageContainer -Context $StorageAccountContext -Name $ContainerName;
#$existingContainer;
if (!$existingContainer)
{
"Could not find storage container";
}
else
{
$containerName = $existingContainer.Name;
Write-Verbose ("Found {0} storage container" -f $containerName);
$blobs = Get-AzureStorageBlob -Container $containerName -Context $StorageAccountContext;
$blobsremoved = 0;
if ($blobs -ne $null)
{
foreach ($blob in $blobs)
{
$lastModified = $blob.LastModified
if ($lastModified -ne $null)
{
#Write-Verbose ("Now is: {0} and LastModified is:{1}" –f [DateTime]::Now, [DateTime]$lastModified);
#Write-Verbose ("lastModified: {0}" –f $lastModified);
#Write-Verbose ("Now: {0}" –f [DateTime]::Now);
$blobDays = ([DateTime]::Now - $lastModified.DateTime) #[DateTime]
Write-Verbose ("Blob {0} has been in storage for {1} days" –f $blob.Name, $blobDays);
Write-Verbose ("blobDays.Days: {0}" –f $blobDays.Hours);
Write-Verbose ("DaysOld: {0}" –f $DaysOld);
if ($blobDays.Days -ge $DaysOld)
{
Write-Verbose ("Removing Blob: {0}" –f $blob.Name);
Remove-AzureStorageBlob -Blob $blob.Name -Container $containerName -Context $StorageAccountContext;
$blobsremoved += 1;
}
else {
Write-Verbose ("Not removing blob as it is not old enough.");
}
}
}
}
Write-Verbose ("{0} blobs removed from container {1}." –f $blobsremoved, $containerName);
}
你可以使用测试窗格输入所需的参数并运行它。
正如你所看到的,当我运行它时,它没有发现任何足够大的 blob 可以删除。