Search code examples
c#azure-storageazure-blob-storage

Azure UploadDirectoryAsync doesn't overwrite existing files if changed


I am doing Directory upload using Microsoft.WindowsAzure.Storage.DataMovement library as below

     TransferManager.Configurations.ParallelOperations = 64;

         UploadDirectoryOptions options = new UploadDirectoryOptions()
                    {                              
                        ContentType = "image/jpeg",
                        Recursive = true,                   

                    };


    context.FileTransferred += FileTransferredCallback;
    context.FileFailed += FileFailedCallback;
    context.FileSkipped += FileSkippedCallback;


    await TransferManager.UploadDirectoryAsync(sourceDir, destDir, options:
 options, context: context, cancellationToken: cts.Token);

I recognized that if I changed the image with the same name, function is ignoring the image and returns exception as file exists. How can I replace the image if datemodified is changed? In the first place of course datemodified on my local pc and azure should be synchronized.


Solution

  • I just figured out Microsoft.WindowsAzure.Storage.DataMovement package has a new update and I installed 0.4.1 version. It looks like that it exposes new methods and events. I am not sure if this was available in the version 0.3 (my previous one) but I only figured out after installing version 0.4.1 Microsoft.WindowsAzure.Storage.DataMovement package. Code below will compare source and destination and decide if should overwrite or not. I hope that it helps anyone else having the same issue.

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