-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab-Functions.ps1
89 lines (71 loc) · 2.85 KB
/
Lab-Functions.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
# Function Add-Entity: Adds a blob file entity of a given type to a table.
function Add-Entity() {
[CmdletBinding()]
param(
$Table,
$Blob,
$Type
)
# Use the Container name as the partition key
$PartitionKey = $Blob.ICloudBlob.Container.Name
# Use the blob name as the row key
$RowKey = $Blob.Name
$Entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity
$Entity.PartitionKey = $PartitionKey
$Entity.RowKey = $RowKey
$Entity.Properties.Add("Type", $Type)
$Entity.Properties.Add("StorageUri", $Blob.ICloudBlob.StorageUri.PrimaryUri.AbsoluteUri)
$Table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($Entity))
}
# Add message to a queue
function Add-Message() {
[CmdletBinding()]
param(
$Queue,
$Blob
)
# Generate read-only SAS token for the blob
$Sas = New-AzureStorageBlobSASToken -CloudBlob $Blob.ICloudBlob -Permission r
# Message contains blob StorageUri and Name (what is needed to generate the thumbnail)
$MessageHashTable = @{ StorageUri = $Blob.ICloudBlob.StorageUri.PrimaryUri;
Sas = $Sas;
Name = "$($Blob.ICloudBlob.Container.Name)\$($Blob.Name)" }
# Convert the hash table to a JSON string
$Message = ConvertTo-Json -InputObject $MessageHashTable
# Construct a CloudQueueMessage with the serialized message
$QueueMessage = New-Object -TypeName Microsoft.WindowsAzure.Storage.Queue.CloudQueueMessage `
-ArgumentList $Message
# Add the message to the queue
$Queue.CloudQueue.AddMessage($QueueMessage)
}
# Process a message if one is available and remove it when complete
function Process-Message() {
[CmdletBinding()]
param(
$Queue
)
# Set the number of seconds to make a queue message invisible while processing
$InvisibleTimeout = [System.TimeSpan]::FromSeconds(10)
# Attempt to get a message from the queue
$QueueMessage = $Queue.CloudQueue.GetMessage($InvisibleTimeout)
if ($QueueMessage -eq $null) {
Write-Host "Empty queue"
return
}
# Deserialize the JSON message
$MessageHashTable = ConvertFrom-Json -InputObject $QueueMessage.AsString
# Download the image to a temp file
$TempFile = [System.IO.Path]::GetTempFileName()
Invoke-WebRequest -Uri "$($MessageHashTable.StorageUri)$($MessageHashTable.Sas)" `
-OutFile $TempFile
# Resize the image
$ThumbnailFile = "$($TempFile)_thumb"
Resize-Image -InputFile $TempFile -OutputFile $ThumbnailFile -Height 100 -Width 300
# Upload the thumbnail to the thumbnails container
Set-AzureStorageBlobContent -File $ThumbnailFile `
-Container thumbnails `
-Blob $MessageHashTable.Name
Write-Host "Finished Processing"
# Delete the message from the queue
$Queue.CloudQueue.DeleteMessage($QueueMessage)
}