Skip to content

Instantly share code, notes, and snippets.

@Kevin-Bronsdijk
Last active July 20, 2016 07:43
Show Gist options
  • Save Kevin-Bronsdijk/5fcd57a14230cf6a79f0acef15bcecb6 to your computer and use it in GitHub Desktop.
Save Kevin-Bronsdijk/5fcd57a14230cf6a79f0acef15bcecb6 to your computer and use it in GitHub Desktop.
Kraken-poweshell batch features
####### Example | Maintain the uri when uploading public images to Azure Blob storage
$files = @('https://kraken.io/assets/images/kraken-logotype.png')
$result = Optimize-ImageUrlToAzure -FileUrl $files -Key $key -Secret $secret -Wait $true `
-AzureAccount $azureAccount -AzureKey $azureKey -AzureContainer $azureContainer -KeepPath $true
$result | format-table
# Container name is test
# --- https://kraken.blob.core.windows.net/test/assets/images/kraken-logotype.png
# Not https://kraken.blob.core.windows.net/test/kraken-logotype.png
####### Example | Maintain the uri when uploading public images to Amazon S3
$files = @('https://kraken.io/assets/images/kraken-logotype.png')
$result = Optimize-ImageUrlToS3 -FileUrl $files -Key $key -Secret $secret -Wait $true `
-AmazonKey $amazonKey -AmazonSecret $amazonSecret -AmazonBucket $amazonBucket -AmazonRegion 'eu-central-1' `
-KeepPath $true
$result | format-table
# --- https://kraken.s3.eu-central-1.amazonaws.com/assets/images/kraken-logotype.png
# Not https://kraken.s3.eu-central-1.amazonaws.com/kraken-logotype.png
####### Example | Public Url, include shared headers and metadata for Azure Blob storage
$headers = @{}
$headers.Add('Cache-Control','max-age=1234')
$metadata = @{}
$metadata.Add('test1','value1')
$files = @($urltest1) # your url's
$result = Optimize-ImageUrlToAzure -FileUrl $files -Key $key -Secret $secret -Wait $true `
-AzureAccount $azureAccount -AzureKey $azureKey -AzureContainer $azureContainer -KeepPath $true `
-Headers $headers -Metadata $metadata
$result | Format-Table
####### Example | Upload images to Amazon S3 + Metadata + Headers + storage path
$headers = @{}
$headers.Add('Cache-Control','max-age=1234')
$items = Get-ChildItem $testimagepath -Filter *.png | `
foreach-object {
$metadata = @{}
$metadata.Add('name',$_.FullName)
$item = New-Object -TypeName Kraken.Powershell.OptimizeImageItem
$item.Path = $_.FullName
$item.Headers = $headers
$item.Metadata = $metadata
$item.ExternalStoragePath = 'hello/'
$item
}
$files = Get-ChildItem $testimagepath -Filter *.png
$result = Optimize-ImageToS3 -OptimizeImageItems $items -Key $key -Secret $secret -Wait $true `
-AmazonKey $amazonKey -AmazonSecret $amazonSecret -AmazonBucket $amazonBucket
$result | Format-Table
####### Script 1 | Process all items, no filters for Azure Blob storage
# Get All Blobs
$context = New-AzureStorageContext $azureAccount -StorageAccountKey $azureKey
$blobs = Get-AzureStorageBlob -Container $azureContainer -Context $context
# Create a list with Azure Storage urls (Only jpeg & png)
$blobsUrl = $blobs | where {($_.ContentType -eq "image/jpeg") -or ($_.ContentType -eq "image/png")} | `
foreach-object { "https://" + $azureAccount + ".blob.core.windows.net/" + $azureContainer + "/" + $_.Name}
# Ask kraken to save the image into Azure Blob storage
$result = Optimize-ImageUrlToAzure -FileUrl $blobsUrl -Key $key -Secret $secret -Wait $true -Lossy $true `
-AzureAccount $azureAccount -AzureKey $azureKey -AzureContainer $azureContainer -KeepPath $true
$result | Format-Table
#######
####### Script 2 | Only process new items
# Get All Blobs
$context = New-AzureStorageContext $azureAccount -StorageAccountKey $azureKey
$blobs = Get-AzureStorageBlob -Container $azureContainer -Context $context
# Create a list with Azure Storage urls (Not processed before and only jpeg & png)
# in addtion, only return items without the kraked metdata entry
$blobsUrl = $blobs | where {($_.ContentType -eq "image/jpeg") -or ($_.ContentType -eq "image/png") `
-and ($_.ICloudBlob.Metadata.ContainsKey("kraked") -eq $false)} | `
foreach-object { "https://" + $azureAccount + ".blob.core.windows.net/" + $azureContainer + "/" + $_.Name}
if ($blobsUrl -eq $null)
{
Write-Host 'No items available'
}
else
{
# Custom Metadata, kraked date or run number
$dateTimeNow = Get-Date -format M.d.yyyy
$metadata = @{}
$metadata.Add('kraked', $dateTimeNow)
$result = Optimize-ImageUrlToAzure -FileUrl $blobsUrl -Key $key -Secret $secret -Wait $true -Lossy $true `
-AzureAccount $azureAccount -AzureKey $azureKey -AzureContainer $azureContainer -KeepPath $true `
-Metadata $metadata
$result | Format-Table
}
#######
####### Script 3 | Only process new items and keep all existing metadata
# Get All Blobs
$context = New-AzureStorageContext $azureAccount -StorageAccountKey $azureKey
$blobs = Get-AzureStorageBlob -Container $azureContainer -Context $context
# Batch date or run
$dateTimeNow = Get-Date -format M.d.yyyy
# Create objects
$imageItems = $blobs | where {($_.ContentType -eq "image/jpeg") -or ($_.ContentType -eq "image/png") `
-and ($_.ICloudBlob.Metadata.ContainsKey("kraked") -eq $false)} |
foreach-object {
# Set headers
$headers = @{}
$headers.Add('Cache-Control','max-age=31536000, public')
# Copy existing Metadata
$metadata = $_.ICloudBlob.Metadata
# Add new metadata
$metadata.Add('kraked', $dateTimeNow)
$metadata.Add('filename', $_.Name)
# Create new bulk object
$item = New-Object -TypeName Kraken.Powershell.OptimizeImageItem
$item.Path = "https://" + $azureAccount + ".blob.core.windows.net/" + $azureContainer + "/" + $_.Name
$item.Headers = $headers
$item.Metadata = $metadata
$item
}
$result = Optimize-ImageUrlToAzure -OptimizeImageItems $imageItems -Key $key -Secret $secret -Wait $true -Lossy $true `
-AzureAccount $azureAccount -AzureKey $azureKey -AzureContainer $azureContainer -KeepPath $true
$result | Format-Table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment