gpt4 book ai didi

azure - 删除 Azure Blob 存储中的 "folder"

转载 作者:行者123 更新时间:2023-12-03 05:49:17 25 4
gpt4 key购买 nike

我想从我的 Azure Blob 存储帐户的容器中删除一个文件夹。该文件包含 3 000 000 多个文件,使用 Azure 存储资源管理器这是一个相当长的过程(1 000 个文件/5 分钟),因此我想知道是否可以立即删除一个文件夹。

我知道 Azure Blob 存储中没有“文件夹”,它更像是访问 Blob 的虚拟路径,但对于大量 Blob 的批量删除,这是有问题的。

最佳答案

Ben 我建议使用这个 Powershell 脚本,一次可以删除 10,000 个:

This PowerShell script, designed to run in Azure Automatiom, deletes huge number of blobs in a container, by processing them in chunks of 10,000 blobs at a time. When the number of blobs grows beyond a couple of thousands, the usual method of deleting each blob at a time may just get suspended without completing the task. This could be used to to delete all blobs (when parameter retentionDays is supplied as 0), or certain blobs which has not been modified for the last rententionDays number of days.

脚本可以在这里下载:https://gallery.technet.microsoft.com/Delete-large-number-of-97e04976

    <#
.Synopsis
Deletes large number of blobs in a container of Storage account, which are older than x days

.DESCRIPTION
This Runbook deletes huge number of blobs in a container, by processing them in chunks of 10,000 blobs at a time. When the number of blobs grow beyond a couple of thousands, the usual method of deleting each blob at a time may just get suspended without completing the task.

.PARAMETER CredentialAssetName
The Credential asset which contains the credential for connecting to subscription

.PARAMETER Subscription
Name of the subscription attached to the credential in CredentialAssetName

.PARAMETER container
Container name from which the blobs are to be deleted

.PARAMETER AzStorageName
The Storage Name to which the container belong to

.PARAMETER retentionDays
Retention days. Blobs older than these many days will be deleted. To delete all, use 0

.NOTES
AUTHOR: Anurag Singh, MSFT
LASTEDIT: March 30, 2016
#>

function delete-blobs
{
param (
[Parameter(Mandatory=$true)]
[String] $CredentialAssetName,

[Parameter(Mandatory=$true)]
[String] $Subscription,

[Parameter(Mandatory=$true)]
[String] $container,

[Parameter(Mandatory=$true)]
[String] $AzStorageName,

[Parameter(Mandatory=$true)]
[Int] $retentionDays
)

$Cred = Get-AutomationPSCredential -Name $CredentialAssetName
$Account = Add-AzureAccount -Credential $Cred

if(!$Account)
{
write-output "Connection to Azure Subscription using the Credential asset failed..."
Break;
}

set-AzureSubscription -SubscriptionName $Subscription

$AzStorageKey = (Get-AzureStorageKey -StorageAccountName $AzStorageName).Primary
$context = New-AzureStorageContext -StorageAccountName $AzStorageName -StorageAccountKey $AzStorageKey


$blobsremoved = 0
$MaxReturn = 10000
$Total = 0
$Token = $Null
$TotalDel = 0
$dateLimit = (get-date).AddDays(-$retentionDays)

try
{
do
{
Write-Output "Retrieving blobs"
$blobs = Get-AzureStorageBlob -Container $container -context $context -MaxCount $MaxReturn -ContinuationToken $Token
$blobstodelete = $blobs | where LastModified -LE $dateLimit
$Total += $Blobs.Count
Write-Output "$Total total Retrieved blobs"

$Token = $Blobs[$blobs.Count -1].ContinuationToken;

if($Blobs.Length -le 0)
{
break;
}

if($blobstodelete.Length -le 0)
{
continue;
}

$TotalDel += $blobstodelete.Count

$blobstodelete | Remove-AzureStorageBlob -Force

Write-Output "$TotalDel blobs deleted"
}
While ($Token -ne $Null)
}

catch
{
write-output $_
}

}

关于azure - 删除 Azure Blob 存储中的 "folder",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49844535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com