gpt4 book ai didi

c# - 如何复制 Azure 表

转载 作者:行者123 更新时间:2023-12-02 21:18:54 25 4
gpt4 key购买 nike

我想制作 Azure 表的副本,但不实际读取其内容。我想知道是否可以制作这样的副本,以及是否可以从 c# 完成

谢谢!

最佳答案

是的,您可以使用 C# 将 Azure 表备份到 Blob 存储。这样做非常有用,例如为灾难恢复场景创建表存储到 Blob 的定期备份。下面是一些代码来做到这一点。它使用 AzCopy ,这是一个有用的批量 Azure 数据移动实用程序,您可以 here 。我将唯一的 GUID 附加到下面的文件名,以防止定期运行此方法覆盖之前复制的 Azure 表。

public void ExportAzureTableToBlobStorage(string locationOfAzCopy, string sourceAccountName,
string sourceKey, string sourceTable, string targetAccountName, string targetAccountKey,
string targetContainerName,
string targetFileName)
{

CreateBlobContainerIfItDoesntExist(targetAccountName, targetAccountKey, targetContainerName);

string uniqueFileIdentifier = Guid.NewGuid().ToString();
string uniqueManifestIdentifier = Guid.NewGuid().ToString();

string uniqueTargetFileName = $"{targetFileName}_{uniqueFileIdentifier}";
string arguments =
$@"/source:https://{sourceAccountName}.table.core.windows.net/{sourceTable} /sourceKey:{sourceKey
}
/dest:https://{targetAccountName}.blob.core.windows.net/{targetContainerName
}/ /Destkey:{targetAccountKey}
/manifest:{uniqueTargetFileName
} /V:.\Files\ExportLog_{uniqueManifestIdentifier}.log /Z:.\Files\ExportJnl_{uniqueManifestIdentifier
}.jnl";

//Configure the process in which to launch AzCopy
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = locationOfAzCopy,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};

proc.Start();
proc.WaitForExit();

while (!proc.StandardOutput.EndOfStream)
{
var runReportMessage = proc.StandardOutput.ReadToEnd();
//Inspect runReportMessage or whatever you need to do with it.
}
}

下面是自动创建指定的 Blob 容器(如果它尚不存在)的方法:

private void CreateBlobContainerIfItDoesntExist(string targetAccountName, string targetAccountKey, string containerName)
{
var blobClient = GetCloudBlobClient(GenerateBlobStorageConnectionString(targetAccountName, targetAccountKey));
var container = blobClient.GetContainerReference(containerName);

//Create a new container, if it does not exist
container.CreateIfNotExists();
}

关于c# - 如何复制 Azure 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28952895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com