如何复制 Azure 表



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

谢谢!

是的,可以使用 C# 将 Azure 表备份到 Blob 存储。这样做非常有用,例如,为灾难恢复方案创建表存储到 Blob 的定期备份。下面是一些代码来做到这一点。它使用 AzCopy,这是一个有用的批量 Azure 数据移动实用工具,可在此处查看。我将唯一的 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:.FilesExportLog_{uniqueManifestIdentifier}.log /Z:.FilesExportJnl_{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();
    }

如果是关于复制 Azure 表实体,可以使用 Azcopy,有关详细信息,请参阅下面的帖子。

http://azure.microsoft.com/en-us/documentation/articles/storage-use-azcopy/#copy-entities-in-an-azure-table-with-azcopy-preview-version-only

最新更新