Azure CLI – 使用模式删除存储帐户中的文件

huangapple go评论49阅读模式
英文:

Azure CLI - Delete files in storage account with pattern

问题

I need to delete all files and directories except the mf folder and its content.
I use this command:

az storage blob delete-batch --source 'CONTAINER' --account-name STORAGE_ACCOUNT --pattern '[!mf]*';

The problem that this command not only avoids deleting the mf directory but also the files that start with the letter m and f.

Is there a way to modify the pattern to include files starting with m and f?

英文:

I have the following files and directories in my container.

Azure CLI – 使用模式删除存储帐户中的文件

I need to delete all files and directories except the mf folder and its content.
I use this command:

az storage blob delete-batch --source 'CONTAINER' --account-name STORAGE_ACCOUNT --pattern '[!mf]*'

The problem that this command not only avoids deleting the mf directory but also the files that start with the letter m and f

Is there a way to modify the pattern to include files starting with m and f?

答案1

得分: 0

我需要删除所有文件和文件夹,除了mf文件夹及其内容。

您可以使用PowerShell作为另一种方法来删除除mf文件夹及其内容之外的所有文件和文件夹。

命令:

$ctx = New-AzStorageContext -StorageAccountName 'venkat123' -StorageAccountKey 'your-storage-account-key'
$containerName = "test"
$prefix = ""
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx -Prefix $prefix
foreach ($blob in $blobs) {
    if ($blob.Name -notlike "mf/*") {
        Remove-AzStorageBlob -Blob $blob.Name -Container $containerName -Context $ctx
    }
}

最初,我有一些文件的结构。

Azure CLI – 使用模式删除存储帐户中的文件

当我使用上述命令时,它会删除所有文件和文件夹,除了mf文件夹。

输出:
Azure CLI – 使用模式删除存储帐户中的文件

门户:
Azure CLI – 使用模式删除存储帐户中的文件

英文:

> I need to delete all files and directories except the mf folder and its content.

You can use the Powershell as an alternative way to achieve delete all files and directories except the mf folder and its content.

Command:

$ctx = New-AzStorageContext -StorageAccountName 'venkat123' -StorageAccountKey 'your-storage-account-key'
$containerName = "test"
$prefix = ""
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx -Prefix $prefix
foreach ($blob in $blobs) {
    if ($blob.Name -notlike "mf/*") {
        Remove-AzStorageBlob -Blob $blob.Name -Container $containerName -Context $ctx
    }
}

Initially, I had a structure of files.

Azure CLI – 使用模式删除存储帐户中的文件

When I use the above command it deletes all files and folders except the mf folder.

Output:
Azure CLI – 使用模式删除存储帐户中的文件

Portal:
Azure CLI – 使用模式删除存储帐户中的文件

答案2

得分: 0

为了继续使用 az cli 命令,我使用了以下命令:

blob=$(az storage blob list -c '$web' --account-name "$SA_NAME" --query "[].{name:name}" --output tsv)
prefix="mf"

for b in $blob
do
  if [[ "$b" != "$prefix"* ]];
  then
    az storage blob delete -c '$web' --account-name "$SA_NAME" -n "$b"
  fi
done
英文:

To continue using az cli commands, I used the following command

blob=$(az storage blob list -c '$web' --account-name "$SA_NAME" --query "[].{name:name}" --output tsv)
prefix="mf"

for b in $blob
do
  if [[ "$b" != "$prefix"* ]];
  then
    az storage blob delete -c '$web' --account-name "$SA_NAME" -n "$b"
  fi
done

答案3

得分: 0

我们可以使用以下命令来删除容器内的文件:

az storage blob delete-batch --account-key <storage_account_key> --account-name <storage_account_name> --source <container_name> --delete-snapshots include

英文:

We can use this command for deleting files inside the container.

az storage blob delete-batch --account-key <storage_account_key> --account-name <storage_account_name> --source <container_name> --delete-snapshots include

huangapple
  • 本文由 发表于 2023年6月9日 11:44:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定