In order to upload files to our azurite storage, we're going to leverage PowerShell... on a mac, so please read the MSDN documentation on that since, it may change.
Though it may seem as if we are adding unnecessary complexity to our project, with powershell we can automate the process of uploading data to our blob storage, which may not seem very important now, however in the future when you we'll be deploying to the cloud, it will make life much simpler.
In your application create a 'powershell' folder, if you haven't already, let's start with a simple powershell script to copy our people.json file from our local hard drive to our docker container.
if(0 -eq ((Get-Module -ListAvailable -Name az).count) ){
Write-host "installing AZ module." -foregroundcolor Yellow
Install-Module -Name Az -Repository PSGallery -Force
Write-host "AZ module installed." -foregroundcolor Yellow
}
else{
Write-host "AZ module already installed." -foregroundcolor Green
}
$cs = "DefaultEndpointsProtocol=https;"
$cs += "AccountName=devstoreaccount1;"
$cs += "AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;"
$cs += "BlobEndpoint=https://127.0.0.1:10000/devstoreaccount1;"
$cs += "QueueEndpoint=https://127.0.0.1:10001/devstoreaccount1;"
$cs += "TableEndpoint=https://127.0.0.1:10002/devstoreaccount1;"
$context = New-AzStorageContext -ConnectionString $cs
function UploadFiles {
Param(
[Parameter(Mandatory=$true)][String]$containerName,
[Parameter(Mandatory=$true)][String]$localPath)
# upload all flat files to local azurite
Get-ChildItem ` -Path $localPath ` -Recurse | `
Set-AzStorageBlobContent `
-Container $containerName `
-Context $context `
}
function createContainer { Param(
[Parameter(Mandatory=$true)][String]$containerName,
[Parameter(Mandatory=$true)][Int32]$permission)
$container = $context | Get-AzStorageContainer -Name $containerName -ErrorAction SilentlyContinue
if($null -ne $container){
Remove-AzStorageContainer -Name $containerName -Context $context
}
$container = $context | Get-AzStorageContainer -Name $containerName -ErrorAction SilentlyContinue
if($null -eq $container){
Write-Host "Createing local $containerName contaier" -ForegroundColor Magenta
$context | New-AzStorageContainer -Name $containerName -Permission $permission
}
else {
Write-Host "local $containerName container already exists" -ForegroundColor Yellow
}
}
createContainer -containerName "flatfiles" -permission 0;
UploadFiles -containerName "flatfiles" -localPath "/Volumes/dev/data/"
$sasToken = New-AzStorageContainerSASToken -name "flatfiles" -Permission "rwdalucp" -Context $context
Write-Host "your SAS token is:$sasToken it has been copied to your clipboard" -ForegroundColor Green
Write-Host "The full connection URL has been copied to your clipboard" -ForegroundColor Green
Write-Output "https://127.0.0.1:10000/devstoreaccount1/flatfiles/people.json?" $sasToken | Set-Clipboard
You should have the URL to the people.json file copied to your clipboard with a SAS token to let you access it, simply past the URL in your browser or postman or insomnia and you should be able to get your json file.
Next let's create a script that will list all of the blobs in our Azurite blob storage
if(0 -eq ((Get-Module -ListAvailable -Name az).count) ){
Write-host "installing AZ module." -foregroundcolor Yellow
Install-Module -Name Az -Repository PSGallery -Force
Write-host "AZ module installed." -foregroundcolor Yellow
}
else{
Write-host "AZ module already installed." -foregroundcolor Green
}
$cs = "DefaultEndpointsProtocol=https;"
$cs += "AccountName=devstoreaccount1;"
$cs += "AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;"
$cs += "BlobEndpoint=https://127.0.0.1:10000/devstoreaccount1;"
$cs += "QueueEndpoint=https://127.0.0.1:10001/devstoreaccount1;"
$cs += "TableEndpoint=https://127.0.0.1:10002/devstoreaccount1;"
$context = New-AzStorageContext -ConnectionString $cs
$containerName = "flatfiles"
Get-AzStorageBlob -Container $containerName -Context $context | Select-Object -Property Name
And finally a powershell script to delete files.
Param (
[Parameter()][String]$blobToDelete,
[Parameter()][Boolean]$deleteAllFiles)
if(0 -eq ((Get-Module -ListAvailable -Name az).count) ){
Write-host "installing AZ module." -foregroundcolor Yellow
Install-Module -Name Az -Repository PSGallery -Force
Write-host "AZ module installed." -foregroundcolor Yellow
}
else{
Write-host "AZ module already installed." -foregroundcolor Green
}
$cs = "DefaultEndpointsProtocol=https;"
$cs += "AccountName=devstoreaccount1;"
$cs += "AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;"
$cs += "BlobEndpoint=https://127.0.0.1:10000/devstoreaccount1;"
$cs += "QueueEndpoint=https://127.0.0.1:10001/devstoreaccount1;"
$cs += "TableEndpoint=https://127.0.0.1:10002/devstoreaccount1;"
$context = New-AzStorageContext -ConnectionString $cs
$containerName = "flatfiles"
if($true -ne ([string]::IsNullOrWhiteSpace($blobToDelete))){
Remove-AzStorageBlob -Blob $blobToDelete -Container $containerName -Context $context
}
if($true -eq $deleteAllFiles) {
Get-AzStorageBlob -Container $containerName -Context $context | Remove-AzStorageBlob
}
the above unlike the other two scripts takes in parameters letting us specify if we want to delete a specific file or all files. it can be called several ways, but the following two are probably the easiest
./deleteBlobs.ps1 people.json
./deleteBlobs.ps1 -deleteAllFiles $true
and that's it for now, keep in mind that theses scripts are specific to our azurite environment, when it comes time to deploy to production we'll cross that bridge when we get to it.