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

时间:2016-03-22 18:31:48

标签: azure azure-storage-blobs azure-powershell

与此问题类似How to get size of Azure CloudBlobContainer

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

3 个答案:

答案 0 :(得分:5)

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

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

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

Get-AzureStorageBlob的输出是一个AzureStorageBlob数组,其中包含一个名为ICloudBlob的属性,您可以在其Properties中获取blob长度,然后就可以求和所有blob都可以获得容器的内容长度。

答案 1 :(得分:2)

以下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”

答案 2 :(得分:1)

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

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,对它进行一些翻转,并使用find输出创建一个要输入blobxfer的文件列表。解决不同的问题,但也许是适合您需求的解决方案。