title: Azure copy blob container contents to another container
body: <p>I'm having some issues getting a blob container to copy over to another container. When the copy is complete all that is in the target location is a file with no extension. there should be a bunch of images from the source container. What I'm trying to achieve is creating a backup of a entire Containers blobs. for my case the Container is 0100, but the actual blobs are in 0100/photos/student/ - so if there is a better way please let me know!</p>
  
  <p><a href="https://i.stack.imgur.com/L5F5J.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/L5F5J.png" alt="target container"></a></p>
  
  <pre><code>      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);
                  }
  
              }
  </code></pre>
  
  <p>For those interested here is the solution on how to copy a Azure blob virtual directory.</p>
  
  <pre><code>     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;
          }
      }
  </code></pre>