如何在 PowerShell 中获取 Azure 容器的大小

How to get size of Azure Container in PowerShell

类似这个问题How to get size of Azure CloudBlobContainer

如何在 PowerShell 中获取 Azure 容器的大小。我可以在 https://gallery.technet.microsoft.com/scriptcenter/Get-Billable-Size-of-32175802 看到建议的脚本,但想知道在 PowerShell

中是否有更简单的方法

以下 PowerShell 脚本是问题 How to get size of Azure CloudBlobContainer 已接受答案中 c# 代码的简单翻译。希望这能满足您的需求。

Login-AzureRmAccount
$accountName = "<your storage account name>"
$keyValue = "<your storage account key>"
$containerName = "<your container name>"

$storageCred = New-Object Microsoft.WindowsAzure.Storage.Auth.StorageCredentials ($accountName, $keyValue)

$storageAccount = New-Object Microsoft.WindowsAzure.Storage.CloudStorageAccount ($storageCred, $true)

$container = $storageAccount.CreateCloudBlobClient().GetContainerReference($containerName)

$length = 0

$blobs = $container.ListBlobs($null, $true, [Microsoft.WindowsAzure.Storage.Blob.BlobListingDetails]::None, $null, $null)

$blobs | ForEach-Object {$length = $length + $_.Properties.Length}

$length

注意:前导 Login-AzureRmAccount 命令将为您加载必要的 .dll。如果您确实知道 "Microsoft.WindowsAzure.Storage.dll" 的路径,则可以将其替换为 [Reflection.Assembly]::LoadFile("$StorageLibraryPath") | Out-Null。路径通常是这样的"C:\Program Files\Microsoft SDKs\Azure.NET SDK\v2.7\ToolsRef\Microsoft.WindowsAzure.Storage.dll"

使用 Azure PowerShell,您可以使用 Get-AzureStorageBlob 以及 Container 和 Context 参数列出容器中的所有 blob,例如:

$ctx = New-AzureStorageContext -StorageAccountName youraccountname -storageAccountKey youraccountkey

$blobs = Get-AzureStorageBlob -Container containername -Context $ctx

Get-AzureStorageBlob 的输出是 AzureStorageBlob 的数组,其中有一个 属性 名称为 ICloudBlob,您可以在其 Properties 中获取 blob 长度,然后您可以将所有 blob 的长度相加以获得容器的内容长度。

这是我今天刚刚敲定的解决方案。上面的例子没有给我我想要的东西,即(1)容器中所有 blob 的字节总和和(2)每个 blob + 路径 + 大小的列表,以便它可用于将结果与 du -b on linux(原点)。

Login-AzureRmAccount
$ResourceGroupName = ""
$StorageAccountName = ""
$StorageAccountKey = ""
$ContainerName = "" 

New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
# Don't NEED the Resource Group but, without it, fills the screen with red as it search each RG...
$size = 0
$blobs = Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -ErrorAction Ignore | Get-AzureStorageBlob -Container $ContainerName
foreach ($blob in $blobs) {$size = $size + $blob.length}
write-host "The container is $size bytes."
$properties = @{Expression={$_.Name};Label="Name";width=180}, @{Expression={$_.Length};Label="Bytes";width=80}
$blobs | ft $properties | Out-String -width 800 | Out-File -Encoding ASCII AzureBlob_files.txt

然后我将文件移动到 Linux 以对其进行一些翻转和查找输出以创建要输入到 blobxfer 的文件列表。不同问题的解决方案,但也许也是适合您需求的解决方案。