This repository was archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathcalculate-container-size-ex.ps1
283 lines (239 loc) · 9.36 KB
/
calculate-container-size-ex.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# this script will show how to get the total size of the blobs in a container
# before running this, you need to create a storage account, create a container,
# and upload some blobs into the container
# note: this retrieves all of the blobs in the container in one command.
# connect Azure with Login-AzAccount before you run the script.
# requests sent as part of this tool will incur transactional costs.
# command line usage: script.ps1 -ResourceGroup {YourResourceGroupName} -StorageAccountName {YourAccountName} -ContainerName {YourContainerName}
#
param(
[Parameter(Mandatory=$true)]
[string]$ResourceGroup,
[Parameter(Mandatory=$true)]
[string]$StorageAccountName,
[Parameter(Mandatory=$true)]
[string]$ContainerName
)
#Set-StrictMode will cause Get-AzStorageBlob returns result in different data types when there is only one blob
#Set-StrictMode -Version 2
$VerbosePreference = "Continue"
if((Get-Module -ListAvailable Az.Storage) -eq $null)
{
throw "Azure Powershell not found! Please install from https://docs.microsoft.com/en-us/powershell/azure/install-Az-ps"
}
# function Retry-OnRequest
function Retry-OnRequest
{
param(
[Parameter(Mandatory=$true)]
$Action)
# It could encounter various of temporary errors, like network errors, or storage server busy errors.
# Should retry the request on transient errors
# Retry on storage server timeout errors
$clientTimeOut = New-TimeSpan -Minutes 15
$retryPolicy = New-Object -TypeName Microsoft.Azure.Storage.RetryPolicies.ExponentialRetry -ArgumentList @($clientTimeOut, 10)
$requestOption = @{}
$requestOption.RetryPolicy = $retryPolicy
# Retry on temporary network errors
$shouldRetryOnException = $false
$maxRetryCountOnException = 3
do
{
try
{
return $Action.Invoke($requestOption)
}
catch
{
if ($_.Exception.InnerException -ne $null -And $_.Exception.InnerException.GetType() -Eq [System.TimeoutException] -And $maxRetryCountOnException -gt 0)
{
$shouldRetryOnException = $true
$maxRetryCountOnException--
}
else
{
$shouldRetryOnException = $false
throw
}
}
}
while ($shouldRetryOnException)
}
# function Get-BlobBytes
function Get-BlobBytes
{
param(
[Parameter(Mandatory=$true)]
$Blob,
[Parameter(Mandatory=$false)]
[bool]$IsPremiumAccount = $false)
# Base + blobname
$blobSizeInBytes = 124 + $Blob.Name.Length * 2
# Get size of metadata
$metadataEnumerator=$Blob.ICloudBlob.Metadata.GetEnumerator()
while($metadataEnumerator.MoveNext())
{
$blobSizeInBytes += 3 + $metadataEnumerator.Current.Key.Length + $metadataEnumerator.Current.Value.Length
}
if (!$IsPremiumAccount)
{
if($Blob.BlobType -eq [Microsoft.Azure.Storage.Blob.BlobType]::BlockBlob)
{
$blobSizeInBytes += 8
# Default is Microsoft.Azure.Storage.Blob.BlockListingFilter.Committed. Need All
$action = { param($requestOption) return $Blob.ICloudBlob.DownloadBlockList([Microsoft.Azure.Storage.Blob.BlockListingFilter]::All, $null, $requestOption) }
$blocks=Retry-OnRequest $action
if ($null -eq $blocks)
{
$blobSizeInBytes += $Blob.ICloudBlob.Properties.Length
}
else
{
$blocks | ForEach-Object { $blobSizeInBytes += $_.Length + $_.Name.Length }
}
}
elseif($Blob.BlobType -eq [Microsoft.Azure.Storage.Blob.BlobType]::PageBlob)
{
# It could cause server time out issue when trying to get page ranges of highly fragmented page blob
# Get page ranges in segment can mitigate chance of meeting such kind of server time out issue
# See https://blogs.msdn.microsoft.com/windowsazurestorage/2012/03/26/getting-the-page-ranges-of-a-large-page-blob-in-segments/ for details.
$pageRangesSegSize = 148 * 1024 * 1024L
$totalSize = $Blob.ICloudBlob.Properties.Length
$pageRangeSegOffset = 0
$pageRangesTemp = New-Object System.Collections.ArrayList
while ($pageRangeSegOffset -lt $totalSize)
{
$action = {param($requestOption) return $Blob.ICloudBlob.GetPageRanges($pageRangeSegOffset, $pageRangesSegSize, $null, $requestOption) }
Retry-OnRequest $action | ForEach-Object { $pageRangesTemp.Add($_) } | Out-Null
$pageRangeSegOffset += $pageRangesSegSize
}
$pageRanges = New-Object System.Collections.ArrayList
foreach ($pageRange in $pageRangesTemp)
{
if($lastRange -eq $Null)
{
$lastRange = New-Object PageRange
$lastRange.StartOffset = $pageRange.StartOffset
$lastRange.EndOffset = $pageRange.EndOffset
}
else
{
if (($lastRange.EndOffset + 1) -eq $pageRange.StartOffset)
{
$lastRange.EndOffset = $pageRange.EndOffset
}
else
{
$pageRanges.Add($lastRange) | Out-Null
$lastRange = New-Object PageRange
$lastRange.StartOffset = $pageRange.StartOffset
$lastRange.EndOffset = $pageRange.EndOffset
}
}
}
$pageRanges.Add($lastRange) | Out-Null
$pageRanges | ForEach-Object {
$blobSizeInBytes += 12 + $_.EndOffset - $_.StartOffset
}
}
else
{
$blobSizeInBytes += $Blob.ICloudBlob.Properties.Length
}
return $blobSizeInBytes
}
else
{
$blobSizeInBytes += $Blob.ICloudBlob.Properties.Length
}
return $blobSizeInBytes
}
# function Get-ContainerBytes
function Get-ContainerBytes
{
param(
[Parameter(Mandatory=$true)]
[Microsoft.Azure.Storage.Blob.CloudBlobContainer]$Container,
[Parameter(Mandatory=$false)]
[bool]$IsPremiumAccount = $false)
# Base + name of container
$containerSizeInBytes = 48 + $Container.Name.Length*2
# Get size of metadata
$metadataEnumerator = $Container.Metadata.GetEnumerator()
while($metadataEnumerator.MoveNext())
{
$containerSizeInBytes += 3 + $metadataEnumerator.Current.Key.Length + $metadataEnumerator.Current.Value.Length
}
# Get size for SharedAccessPolicies
$containerSizeInBytes += $Container.GetPermissions().SharedAccessPolicies.Count * 512
# Calculate size of all blobs.
$blobCount = 0
$Token = $Null
$MaxReturn = 5000
do {
$Blobs = Get-AzStorageBlob -Context $storageContext -Container $Container.Name -MaxCount $MaxReturn -ContinuationToken $Token
if($Blobs -eq $Null) { break }
#Set-StrictMode will cause Get-AzStorageBlob returns result in different data types when there is only one blob
if($Blobs.GetType().Name -eq "AzureStorageBlob")
{
$Token = $Null
}
else
{
$Token = $Blobs[$Blobs.Count - 1].ContinuationToken;
}
$Blobs | ForEach-Object {
$blobSize = Get-BlobBytes $_ $IsPremiumAccount
$containerSizeInBytes += $blobSize
$blobCount++
if(($blobCount % 1000) -eq 0)
{
Write-Verbose("Counting {0} Sizing {1} " -f $blobCount, $containerSizeInBytes)
}
}
}
While ($Token -ne $Null)
return @{ "containerSize" = $containerSizeInBytes; "blobCount" = $blobCount }
}
#Login-AzAccount
$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroup -Name $StorageAccountName -ErrorAction SilentlyContinue
if($storageAccount -eq $null)
{
throw "The storage account specified does not exist in this subscription."
}
$storageContext = $storageAccount.Context
if (-not ([System.Management.Automation.PSTypeName]'PageRange').Type)
{
$Source = "
public class PageRange
{
public long StartOffset;
public long EndOffset;
}"
Add-Type -TypeDefinition $Source
}
$containers = New-Object System.Collections.ArrayList
if($ContainerName.Length -ne 0)
{
$container = Get-AzStorageContainer -Context $storageContext -Name $ContainerName -ErrorAction SilentlyContinue |
ForEach-Object { $containers.Add($_) } | Out-Null
}
else
{
Get-AzStorageContainer -Context $storageContext | ForEach-Object { $containers.Add($_) } | Out-Null
}
$sizeInBytes = 0
$IsPremiumAccount = ($storageAccount.Sku.Tier -eq "Premium")
if($containers.Count -gt 0)
{
$containers | ForEach-Object {
Write-Output("Calculating container {0} ..." -f $_.CloudBlobContainer.Name)
$result = Get-ContainerBytes $_.CloudBlobContainer $IsPremiumAccount
$sizeInBytes += $result.containerSize
Write-Output("Container '{0}' with {1} blobs has a sizeof {2:F2} MB." -f $_.CloudBlobContainer.Name,$result.blobCount,($result.containerSize/1MB))
}
}
else
{
Write-Warning "No containers found to process in storage account '$StorageAccountName'."
}