2

Similar to this question How to get size of Azure CloudBlobContainer

How can one get the size of the Azure Container in PowerShell. I can see a suggested script at https://gallery.technet.microsoft.com/scriptcenter/Get-Billable-Size-of-32175802 but want to know if there is a simpler way to do in PowerShell

Community
  • 1
  • 1
TechnoFobic
  • 63
  • 2
  • 6

3 Answers3

5

With Azure PowerShell, you can list all blobs in the container with Get-AzureStorageBlob with Container and Context parameter like:

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

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

Output of Get-AzureStorageBlob is an array of AzureStorageBlob, which has a property with name ICloudBlob, you can get blob length in its Properties, then you can sum length of all blobs to get content length of the container.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
EmmaZhu-MSFT
  • 109
  • 4
  • 3
    A little warning: if a blob container is too large, you will eventually get an OutOfMemoryException using this code. You can solve this by implementing MaxCount and ContinuationToken to retrieve the blobs in batches – Hanno Jan 11 '17 at 15:18
2

The following PowerShell script is a simple translation of the c# code in the accepted answer of the question How to get size of Azure CloudBlobContainer. Hope this suit your needs.

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

Note: the leading Login-AzureRmAccount command will load the necessary .dll for you. If you do know the path of "Microsoft.WindowsAzure.Storage.dll", you can replace it by [Reflection.Assembly]::LoadFile("$StorageLibraryPath") | Out-Null. The path is usually like this "C:\Program Files\Microsoft SDKs\Azure.NET SDK\v2.7\ToolsRef\Microsoft.WindowsAzure.Storage.dll"

Community
  • 1
  • 1
Jack Zeng
  • 2,257
  • 12
  • 23
  • Thanks that gives a good idea. I have avoided Login-AzureRmAccount by using the storage account key and context – TechnoFobic Mar 24 '16 at 00:20
  • 1
    The `Login-AzureRmAccount` is not for login at all. It's for loading the library. It's not necessary if you know the path of the .dll file. @EmmaZhu-MSFT's answer is greate. She's using pure PowerShell commands. And, my answer is showing that if you get a piece of C# code, you always can translate it into a PowerShell script. Another way to solve the problem. – Jack Zeng Mar 24 '16 at 00:46
  • I wanted to upvote your answer but I don't have enough points yet to do that:) – TechnoFobic Mar 24 '16 at 01:05
  • No problem. @EmmaZhu-MSFT's answer is already accepted. Her answer is also great. – Jack Zeng Mar 24 '16 at 01:08
1

Here's my solution I just hammered through today. Above examples didn't give me what I wanted which was (1) a byte sum of all blobs in a container and (2) a list of each blob + path + size so that it can be used to compare the results to a 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

I then moved the file to Linux to do some flip flopping of it and the find output to create a list of files to input into blobxfer. Solution to a different problem, but perhaps a suitable solution for your needs as well.

Cantaberry
  • 21
  • 3