Azure将blob容器内容复制到另一个容器

时间:2016-11-10 15:58:43

标签: c# azure-storage-blobs

我在将blob容器复制到另一个容器时遇到了一些问题。复制完成后,目标位置中的所有内容都是没有扩展名的文件。源容器中应该有一堆图像。我想要实现的是创建整个Containers blob的备份。对于我的情况,容器是0100,但实际的blob是0100 /照片/学生/ - 所以如果有更好的方法请告诉我!

target container

      string sBlobConnString = ConfigurationManager.AppSettings["SMARTBlobStorageConn"];

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(sBlobConnString);

            //create the blob client
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer container = blobClient.GetContainerReference(srcContainer);
            if (container.Exists())
            {

                CloudBlobDirectory source = container.GetDirectoryReference(blobName);

                var target = container.GetBlockBlobReference(destBlobName);



                var blobs = source.ListBlobs();
                foreach (var blob in blobs)
                {
                    target.StartCopyFromBlob(blob.Uri);
                    copyStates.Add(target.CopyState);
                }

            }

对于那些感兴趣的人来说,是关于如何复制Azure blob虚拟目录的解决方案。

     public static void CopyDirectory(string srcContainer, string srcDirectory, string destContainer, string destDirectory)
    {
        CloudBlobDirectory sourceBlobDir = GetCloudBlobDirectory(srcContainer, srcDirectory);
        CloudBlobDirectory destBlobDir = GetCloudBlobDirectory(destContainer, destDirectory);

        CopyDirectoryOptions options = new CopyDirectoryOptions()
        {                
            Recursive = true,
        };

        TransferContext context = new TransferContext();           

        // Create CancellationTokenSource used to cancel the transfer
        CancellationTokenSource cancellationSource = new CancellationTokenSource();

        try
        {
            TransferManager.CopyDirectoryAsync(sourceBlobDir, destBlobDir, false /* isServiceCopy */, options, context, cancellationSource.Token);            
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }

0 个答案:

没有答案