Lab steps 2 – Practice Labs –Implementing and Managing Storage

Generate Secure Access Tokens (SAS tokens) for both storage accounts and note them. Now that your function has been created, click Code + Test on the left menu and paste the following code, replacing the $SrcSASToken text with the SAS token from the storage account in the Az104-07-rg0 resource group, and replacing the $DstSASToken text with the SAS token from the storage account in the Az104-07-rg1 resource group. Also, replace StorageAccountName1 with the name of the storage account in the Az104-07-rg0 resource group, and replace StorageAccountName2 with the name of the storage account in the Az104-07-rg1 resource group. Click Save once the code is modified:

#Input bindings are passed in via param block. param([byte[]] $InputBlob, $TriggerMetadata)

#Define variables

$SrcStgAccURI = “https://StorageAccountName1.blob.core.windows. net/”

$SrcBlobContainer = “az104-07-container”

$SrcSASToken = “YourSASToken”

$SrcFullPath =

“$($SrcStgAccURI)$($SrcBlobContainer)/$($SrcSASToken)”

$DstStgAccURI = “https://StorageAccountName2.blob.core.windows. net/”

$DstBlobContainer = “az104-07-container”

$DstSASToken = “YourSASToken”

$DstFullPath =

“$($DstStgAccURI)$($DstBlobContainer)/$($DstSASToken)”

  • Test if AzCopy.exe exists in current folder $WantFile = “azcopy.exe”

$AzCopyExists = Test-Path $WantFile

  • Download AzCopy if it doesn’t exist

If ($AzCopyExists -eq $False) {

Write-Host “AzCopy not found. Downloading…”;

Invoke-WebRequest -Uri “https://aka.ms/downloadazcopy-v10-windows” -OutFile AzCopy.zip -UseBasicParsing

Expand-Archive ./AzCopy.zip ./AzCopy -Force # Copy AzCopy to current dir

Get-ChildItem ./AzCopy/*/azcopy.exe | Copy-Item -Destination “./AzCopy.exe”

}

else { Write-Host “AzCopy found, skipping download.” }

# Run AzCopy from source blob to destination file share

Write-Host “Backing up storage account…”

$env:AZCOPY_JOB_PLAN_LOCATION = $env:temp+’\.azcopy’ $env:AZCOPY_LOG_LOCATION=$env:temp+’\.azcopy’

./azcopy.exe copy $SrcFullPath $DstFullPath –overwrite=ifsourcenewer –recursive

Top Tip

The following lines of code change the plan and log file paths for the AzCopy application. This prevents errors from occurring when running the application in the function, as they cannot write to the default folder locations:

$env:AZCOPY_JOB_PLAN_LOCATION = $env:temp+’\.

azcopy’

$env:AZCOPY_LOG_LOCATION=$env:temp+’\.azcopy’

Task six: Upload some data to the source blob container.

We will now upload some data to the source blob container.

Navigate to the storage account in the Az104-07-rg0 resource group, click on Containers on the left menu, clickaz104-07-container, and click Upload to uploada file to the container:

Figure 8.5 – The source container

Let’s now proceed to the last task.

Task seven: Confirm replication has occurred.

We will now confirm the replication script is working as expected.

Navigate to the storage account in the Az104-07-rg1 resource group, click on Containers on the left menu, clickaz104-07-container, and note the files in the destination container. These should now emulate the source container, thereby confirming working replication:

Figure 8.6 – The destination container

You have now demonstrated the replication of data using Azure Functions and AzCopy to replicate data between two different storage accounts and blob containers triggered through an upload operation. Based on the operation’s success, you have determined that AzCopy can be utilized through functions to meet your company requirements. You will now need to build a replication strategy to align with business requirements before implementing a production solution.

The lab is completed; you can now remove all the associated resources for the lab.

Top Tip

While performing this exercise, you may have noticed the option on

a storage account to configure replication. This replication option replicates all transactions and creates what is known as a mirror of the storage on the source in the destination storage account. Using the AzCopy exercise, you can create your own governance mechanisms on functions to ensure that data is copied as it’s initially loaded and never deleted or modified by using AzCopy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post