c# - 如何复制 Azure 表

标签 c# azure azure-storage azure-table-storage

我想制作 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/

相关文章:

azure - CloudBlobClient.startCopyAsync() 的源代码

c# - 提高 Azure Blob 存储查询速度

c# - 将 ProperCase 转换为 Title Case 中的句子(而不是句子大小写)

c# - 为什么 val += someOtherValue 这么糟糕?

c# - ASP.NET MVC 应用程序无法访问路径错误

php - 如何在 Microsoft 中开发离线应用程序而不会使其刷新 token 在 90 天后过期

azure - 如何使用参数调用azure函数

azure - 应用程序服务应用程序设置未被覆盖

azure - 将数据从 azure blob 存储传输到 hdfs 文件系统

c# - C#非永久性“静态”关键字?