天天看点

using azure functions blob trigger and azcopy copy blob from different subscription

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:

https://www.51azure.cloud/post/2020/7/21/using-azure-functions-blob-trigger-and-azcopy-copy-blob-from-source-storage-account-to-destination-in-different-subscription

steps:

1. create source storage and container:

using azure functions blob trigger and azcopy copy blob from different subscription

2. create destination storage and container:

using azure functions blob trigger and azcopy copy blob from different subscription

3. crate sas token in both source and destination:

using azure functions blob trigger and azcopy copy blob from different subscription

4. using vs code create azure functions:

language: powershell

using azure functions blob trigger and azcopy copy blob from different subscription

trigger:blob trigger

using azure functions blob trigger and azcopy copy blob from different subscription

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.

using azure functions blob trigger and azcopy copy blob from different subscription

when shown these messages, means almost finish install:

using azure functions blob trigger and azcopy copy blob from different subscription

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

using azure functions blob trigger and azcopy copy blob from different subscription

after local functions finished running, check dest:

using azure functions blob trigger and azcopy copy blob from different subscription

7. deploy functions to cloud:

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

using azure functions blob trigger and azcopy copy blob from different subscription

copy the config setting from local.setting.json:

using azure functions blob trigger and azcopy copy blob from different subscription

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

using azure functions blob trigger and azcopy copy blob from different subscription

继续阅读