SalimAzureBlobAPI/SalimAzureAPI/AzureBlobManager.cs
/// <summary>
/// Moves a Blob directory from one location to another within the same container. 
/// If the target directory exists prior to the copy, it will be deleted first. 
/// </summary>
/// <param name="containerName">The blob container to move to. If the container does not exist it will be created.</param>
/// <param name="sourceDirectory">The source blob directory to copy the blobs from. Should end with a "/" character.</param>
/// <param name="targetDirectory">The target blob directory to copy the blobs to. Should end with a "/" character. If it
/// already exists, it will be deleted, along with any blobs already contained within.</param>
/// <returns>true indicating success; false otherwise.</returns>
Moves a Blob directory from one location to another within the same container.
If the target directory exists prior to the copy, it will be deleted first.
Moves a Blob directory from one location to another within the same container . If the target directory exists prior to the copy it will be deleted first .
public bool MoveBlobDirectory(string containerName, string sourceDirectory, string targetDirectory)
        {
            bool returnValue = true;
            ExecuteWithExceptionHandling(
                   () =>
                   {
                       if (!DoesContainerExist(containerName))
                       {
                           CreateContainer(containerName);
                       }
                       CloudBlobContainer container = _blobClient.GetContainerReference(containerName);
                       if (DoesBlobDirectoryExist(containerName, targetDirectory))
                       {
                           if (!DeleteBlobDirectory(containerName, targetDirectory))
                               returnValue = false;
                       }
                       if (returnValue)  
                       {
                           Parallel.ForEach(container.ListBlobs(sourceDirectory, false), item =>
                           {
                               string sourceBlobName = string.Empty;
                               string targetBlobName = string.Empty;
                               CloudBlockBlob sourceBlob;
                               CloudBlockBlob targetBlob;
                               try
                               {
                                   if (item.GetType() == typeof(CloudBlob) || item.GetType().BaseType == typeof(CloudBlob))
                                   {
                                       sourceBlobName = ((CloudBlob)item).Name;
                                       sourceBlob = container.GetBlockBlobReference(sourceBlobName);
                                       targetBlobName = ((CloudBlob)item).Name.Replace(((CloudBlob)item).Parent.Prefix, String.Empty);
                                       targetBlob = container.GetBlockBlobReference(targetDirectory + targetBlobName);
                                       targetBlob.StartCopy(sourceBlob);
                                       DeleteBlob(containerName, sourceBlobName);
                                   }
                               }
                               catch (Exception e)
                               {
                                   returnValue = false;
                               }
                           }    
                           );   
                       }
                   });
            return returnValue;
        }
public bool MoveBlobDirectory ( string containerName , string sourceDirectory , string targetDirectory ) { bool returnValue = true ; ExecuteWithExceptionHandling ( ( ) => { if ( ! DoesContainerExist ( containerName ) ) { CreateContainer ( containerName ) ; } CloudBlobContainer container = _blobClient . GetContainerReference ( containerName ) ; if ( DoesBlobDirectoryExist ( containerName , targetDirectory ) ) { if ( ! DeleteBlobDirectory ( containerName , targetDirectory ) ) returnValue = false ; } if ( returnValue ) { Parallel . ForEach ( container . ListBlobs ( sourceDirectory , false ) , item => { string sourceBlobName = string . Empty ; string targetBlobName = string . Empty ; CloudBlockBlob sourceBlob ; CloudBlockBlob targetBlob ; try { if ( item . GetType ( ) == typeof ( CloudBlob ) || item . GetType ( ) . BaseType == typeof ( CloudBlob ) ) { sourceBlobName = ( ( CloudBlob ) item ) . Name ; sourceBlob = container . GetBlockBlobReference ( sourceBlobName ) ; targetBlobName = ( ( CloudBlob ) item ) . Name . Replace ( ( ( CloudBlob ) item ) . Parent . Prefix , String . Empty ) ; targetBlob = container . GetBlockBlobReference ( targetDirectory + targetBlobName ) ; targetBlob . StartCopy ( sourceBlob ) ; DeleteBlob ( containerName , sourceBlobName ) ; } } catch ( Exception e ) { returnValue = false ; } } ) ; } } ) ; return returnValue ; }
Moves a Blob directory from one location to another within the same container.
Moves a Blob directory from one location to another within the same container .
//Check the container exists. If not, create it. //see if the target directory exists //if so, see if we can delete it //no issues thus far. That's good.  //copy the files over in parallel //foreach (IListBlobItem item in container.ListBlobs(sourceDirectory, false)) //These are declared inside the loop for multi-threading purposes. //Set the new blob name, replacing the prefix with an  //empty string to just get the name of the file. //The prefix contains the virtual folder path. //Something happened, so assume not successful. //for loop //parallel
true indicating success; false otherwise.
true indicating success ; false otherwise .
containerName
string
The blob container to move to. If the container does not exist it will be created.
The blob container to move to . If the container does not exist it will be created .
sourceDirectory
string
The source blob directory to copy the blobs from. Should end with a "/" character.
The source blob directory to copy the blobs from . Should end with a " / " character .
targetDirectory
string
The target blob directory to copy the blobs to. Should end with a "/" character. If it
already exists, it will be deleted, along with any blobs already contained within.
The target blob directory to copy the blobs to . Should end with a " / " character . If it already exists it will be deleted along with any blobs already contained within .