locked
TransferManager.CopyDirectoryAsync is not replacing the files RRS feed

  • Question

  • Hi  Team,

    In One of our project we have requirment every time we need to copy the files to output blobs,if files are already present then we need replace the  files

    I wrote the flowing code. 

       var Container = _blobClient.GetContainerReference(ContainerName);
                    CloudBlobDirectory source = Container.GetDirectoryReference(Source);
                    CloudBlobDirectory dest = Container.GetDirectoryReference(Target);

    await TransferManager.CopyDirectoryAsync(source, dest, true, new CopyDirectoryOptions { Recursive = true }, new DirectoryTransferContext());

    This is works fine if files are not  there in the storage ,but files already present in storage ,Its not replacing the files.Could you please help here.

    we are using  Microsoft.WindowsAzure.Storage.DataMovement library for above purpose.

    And also please suggest any improvement in above code.

    Wednesday, July 18, 2018 1:02 PM

All replies

  • Code below will compare source and destination and decide if should overwrite or not. See if it helps.

    context.ShouldOverwriteCallback = (source, destination) =>
    {
        var sourceFile = new FileInfo((string)source);
        var destBlob = destination as CloudBlob;
        return sourceFile.LastWriteTimeUtc > destBlob.Properties.LastModified;
    };
    

    Refer: https://stackoverflow.com/questions/41043925/azure-uploaddirectoryasync-doesnt-overwrite-existing-files-if-changed

    --------------------------------------------------------------------------------------------------

    If this answer was helpful, click “Mark as Answer” or Up-Vote. To provide additional feedback on your forum experience, click here

    • Proposed as answer by Sandeep BR Wednesday, July 18, 2018 5:03 PM
    Wednesday, July 18, 2018 5:03 PM
  • we dont have file  names here,only  i have  blob directory , I need to overwrite everytime the directory 
    Thursday, July 19, 2018 12:40 PM
  • Have you tried UploadByteArray() . It will overwrite the blob that is already there.

    Friday, July 20, 2018 9:13 PM
  • @Raghu :

    Just checking in if you have had a chance to see the previous response

    Tuesday, July 24, 2018 5:31 PM
  • actually i want it for blob directory but its for blob 

    what is source is here ? destination here 

    context.ShouldOverwriteCallback = (source, destination) =>
    {
        var sourceFile = new FileInfo((string)source);
        var destBlob = destination as CloudBlob;
        return sourceFile.LastWriteTimeUtc > destBlob.Properties.LastModified;
    };

    This is my method

     public async void CopyBlob(string Source,string Target)
            {
                try
                {

                    if (string.IsNullOrEmpty(ContainerName))
                        ContainerName = _settings.ContainerName;
                    var Container = _blobClient.GetContainerReference(ContainerName);
                    CloudBlobDirectory source = Container.GetDirectoryReference(Source);
                    CloudBlobDirectory dest = Container.GetDirectoryReference(Target);
                    TransferManager.Configurations.ParallelOperations = 64;


                    if (IsBlobDelete)
                    {
                        var blobList = dest.ListBlobs();
                        if (blobList.Count() > 0)
                        {
                            foreach (var blob in blobList)
                            {

                                if (blob is CloudBlob cloudBlob)
                                    await cloudBlob.DeleteAsync();
                            }
                        }
                    }
                    await TransferManager.CopyDirectoryAsync(source, dest, true, new CopyDirectoryOptions { Recursive = true }, new DirectoryTransferContext());
                  
                }
                catch (System.Exception ex)
                {
                    throw new FieldException(FieldException.BuildError(ex.Message, Enums.ErrorCodes.BlobUploadFail));
                }
            }

    Saturday, July 28, 2018 7:12 PM