This  blob sharing topic:

using azure functions blob trigger and azcopy copy blob from source storage account to destination in different subscription;

This is also work copy blob from global subscription and Azure China( mooncake azure );

 

 

本文介绍:

在不同的订阅间,使用 azure functions blob trigger 和 azcopy 增量同步 blob,本方案同样适用于同步到 azure file share。 

 

videos:

 

steps:

1. create source storage and container:

2. create destination storage and container:

 

3. crate sas token in both source and destination:

 

4. using vs code create azure functions:

language: powershell

trigger:blob trigger

functions source code:

please using your  own parameters!

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

# Write out the blob name and size to the information log.
Write-Host "PowerShell Blob trigger function Processed blob! Name: $($TriggerMetadata.Name) Size: $($InputBlob.Length) bytes"


# Define source, source contains the blob newly uploaded(with file name)
$SourceURI = "https://storagesubsource.blob.core.windows.net/"
$SourceBlobContainer = "source/"
$SourceSASToken = "?sv=2019-10-10&ss=bfqt&srt=sco&sp=rwdlacupx&se=2021-02-28T20:34:03Z&st=2020-07-20T12:34:03Z&spr=https&sig=Qp1SkeSEdQbscC2SjseVVHM3ae4eMThF2dqGVRShlns%3D"
$SourceFullPath = "$($SourceURI)$($SourceBlobContainer)$($TriggerMetadata.Name)$($SourceSASToken)"


# Define dest, dest is only the full container name
$DestStgAccURI = "https://storagesubdest.blob.core.windows.net/"
$DestBlobContainer = "dest/"
$DestSASToken = "?sv=2019-10-10&ss=bfqt&srt=sco&sp=rwdlacupx&se=2020-08-20T20:37:12Z&st=2020-07-20T12:37:12Z&spr=https&sig=TDuc6kB%2FHIgxAXJeujU4cQQcXYxfjBIEbl25YoH3FWA%3D"
$DestFullPath = "$($DestStgAccURI)$($DestBlobContainer)$($DestSASToken)"



# del azcopy.exe and download lastest version of azcopy
# del azcopy.exe

# Test if AzCopy.exe exists in current folder
$AzcoypFile = "azcopy.exe"
$AzCopyExists = Test-Path $AzcoypFile
Write-Host "AzCopy exists:" $AzCopyExists

# Download AzCopy.zip and unzip if it doesn't exist
If ($AzCopyExists -eq $False)
{
    Write-Host "AzCopy not found. Downloading..."
    
    #Download AzCopy
    Invoke-WebRequest -Uri "https://aka.ms/downloadazcopy-v10-windows" -OutFile AzCopy.zip -UseBasicParsing
 
    #unzip azcopy
    write-host "unzip azcopy.zip"
    Expand-Archive ./AzCopy.zip ./AzCopy -Force

    # Copy AzCopy to current dir
    Get-ChildItem ./AzCopy/*/azcopy.exe | Copy-Item -Destination "./AzCopy.exe"
}


# Run AzCopy from source blob to destination blob 

Write-Host "copy blob from  $($SourceFullPath) to  $($DestFullPath)"
./azcopy.exe copy $SourceFullPath $DestFullPath --recursive=true

 

5. run functions on your dev machine:

note: you may need install powershell on your local, just follow the notification message,

when you find the message below, you maybe need waiting for about 3~5 mins, powershell is installing az module.

 

when shown these messages, means almost finish install:

6. upload file to source blob, check dest blob and functions log:

 

after local functions finished running, check dest:

7. deploy functions to cloud:

ctrl+shift+p, deploy will create new resource group and resources by default:

 

copy the config setting from local.setting.json:

 

8. stop the function on your local machine, upload file to source blob, check dest blob and functions log on azure:

note: the log may be listed here later 1~3 mins