-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-blobs.ps1
82 lines (69 loc) · 1.94 KB
/
upload-blobs.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
param(
[Parameter(Mandatory)]
[string]
$AccountName,
[Parameter(Mandatory)]
[string]
$AccountKey,
[Parameter(Mandatory)]
[string]
$Container,
[Parameter()]
[ValidateSet(1, 10, 100, 1000)]
[Int32]
$BatchSize = 1,
[Parameter()]
[ValidateRange(1, 50)]
[Int32]
$BatchCount = 1,
[switch]
$DryRun
)
function Assert-Success([string]$ErrorMessage) {
if (!$?) {
throw $ErrorMessage
}
}
$ErrorActionPreference = "Stop";
Write-Host -ForegroundColor Cyan "Uploading $BatchCount batches of $BatchSize blobs to Storage"
# Detect login/token expiration here
$acct = az account get-access-token;
if ($ForceLogin -or !$acct) {
Write-Host $(If ($ForceLogin) { "" } else { "Not logged in (or token expired) -" }) "Logging in now..."
az login | Out-Null
Assert-Success "Error Logging In."
}
# upload here
$pattern = "";
switch ($BatchSize) {
1 { $pattern = "img0000.png" }
10 { $pattern = "img000?.png" }
100 { $pattern = "img00??.png" }
1000 { $pattern = "img0???.png" }
default {
throw "No Match Found for Batch Size $BatchSize"
}
}
Write-Host "Each batch ($BatchCount batches) is Uploading $BatchSize records with pattern '$pattern'"
1..$BatchCount | ForEach-Object -Parallel {
if ($using:DryRun) {
Write-Output "Dry Run, skipping upload $_."
}
else {
try {
# Write-Output "Uploading Batch $_`n"
az storage blob upload-batch `
--auth-mode key `
--account-name $using:AccountName `
--account-key $using:AccountKey `
-d $using:Container `
-s ./blobs `
--pattern $using:pattern `
| Out-Null
# $using:Assert-Success "Error Uploading Blobs"
}
catch {
Write-Output "Error Uploading", $_
}
}
} -ThrottleLimit 6 -AsJob | Wait-Job | Receive-Job