如何在预定时间同步两个不同帐户上的两个 Azure blob?

How to sync two Azure blobs on two different accounts on schedule time?

为了实现高可用性,我们在不同 Azure 区域的不同存储帐户下创建了两个 blob 容器。

因此,如果应用程序在 WRITING 到主 blob 容器时发现问题,应用程序将执行断路器逻辑,如果即使尝试阈值次数后问题仍然存在,应用程序将启动正在写入 到位于不同 Azure 位置的备用 blob 存储帐户,此架构工作正常。

用于从主要切换到次要的代码:

AsyncLazy<CloudQueue> qClient = new AsyncLazy<CloudQueue>(async () =>
{
    var myStorageAccount = CloudStorageAccount.Parse("ConnectionString");
    var myQueue = myStorageAccount.CreateCloudQueueClient();
    myQueue.DefaultRequestOptions = new QueueRequestOptions
    {
        RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(3), 4),
        
        LocationMode = LocationMode.PrimaryThenSecondary,
        
        MaximumExecutionTime = TimeSpan.FromSeconds(20)
    };
    var queue = myQueue.GetQueueReference("QueueName");
    await queue.CreateIfNotExistsAsync();
    return queue;
});

现在的问题是,一旦主帐户恢复运行,如何将写入备用位置的数据与主位置中的实际 blob 同步回?

Azcopy 是在两个不同帐户上同步两个 blob 的一个选项,但问题是根据这个 link 官方 docker Azcopy 图像尚不可用

Azcopy 的官方 docker 图像可用吗?如果不能,还有哪些其他合适的选项可以在预定时间同步两个不同帐户上的两个 blob? Azure 数据工厂? Azure 函数?

首先,如您所知,没有可用的 Azcopy 官方 docker 图像。 github 问题提到了它。

是的,你可以使用 azure 函数来完成(关于 ADF,不确定,但问了一些人,他们说这样做并不容易),但可能有点困难。

更简单的解决方案是同时使用 azure web jobazcopy。在 Azure 门户中创建时,只需将 Web 作业指定为计划。 Azure webjob 支持许多文件类型,例如 .ps1(powershell), .cmd, .py 等。因此,使用您最喜欢的一种来创建它非常容易。

在这里,我将创建一个.ps1(powershell) 文件,然后将其上传到azure webjob 以在预定时间执行同步作业。

步骤 1:创建一个 .ps1 文件。 文件名必须是运行.ps1。然后在运行.ps1文件中使用下面的代码(请在代码中使用自己的源存储和目标存储):

#define the source container and destination container, note that the sas token are required.
$source_container = "https://yy1.blob.core.windows.net/a11?sv=2020-02-10&ss=bfqt&srt=sco&sp=rwdlacup&se=2021-03-12T11:00:50Z&st=2021-03-12T03:00:50Z&spr=https&sig=xxxxxx"
$dest_container = "https://yyasia1.blob.core.windows.net/test123?sv=2020-02-10&ss=bfqt&srt=sco&sp=rwdlacup&se=2021-03-12T11:01:36Z&st=2021-03-12T03:01:36Z&spr=https&sig=xxxxxx"

#get the current working directory which contains the current .ps1 file and the azcopy.exe
$path = Split-Path -Parent $MyInvocation.MyCommand.Definition

#set the location to the path where contains the azcopy.exe
Set-Location -Path $path

#execute the sync command
.\azcopy.exe sync $source_container $dest_container --recursive

Write-Output "**completed**"

第2步:将azcopy.exe(如果没有,请先下载)放在run.ps1相同的位置文件。然后将这2个文件压缩成一个.zip文件(注意:在压缩它们之前,你最好在本地测试你的代码,看看它是否可以工作):

第 3 步:假设您已经有一个支持 webjob 的 azure web 应用程序服务并且启用了 always on 功能。导航到 azure 门户 -> 你的 azure web 应用程序 -> 设置 -> Webjobs -> 单击“添加”按钮 -> 在“添加 Webjob”面板中,填写所有必要的字段/选择 .zip 文件/并设置正确的类型/触发器/CRON。这是屏幕截图:

第四步:创建webjob后(可能需要几分钟,点击“刷新”按钮查看),selectwebjob,然后单击“运行”按钮: