Azure Blob Storage StartCopyFromUriAsync() Causes Zero Byte Files to be Copied

28 Views Asked by At

I am copying some files from Azure Media Services to Azure blob storage and a lot of files get copied successfully but periodically some files get copied over and have a zero byte file size. I'm not sure why this would happen for some files and not others? They are all being copied over via URL from Azure Media Services.

var blob = container.GetBlobClient(fileName);
var operation = blob.StartCopyFromUriAsync(new Uri(url));
var fileSize = await operation.WaitForCompletionAsync(
    DelayStrategy.CreateExponentialDelayStrategy(
        initialDelay: TimeSpan.FromSeconds(1), maxDelay: TimeSpan.FromMinutes(5)),
    CancellationToken.None);

// fileSize == 0 sometimes!
1

There are 1 best solutions below

1
Venkatesan On

Azure Media Services to Azure Blob Storage and a lot of files get copied successfully, but periodically some files get copied over and have a zero-byte file size.

According to this MS-Documentation, the problem with the StartCopyFromUriAsync method when copying zero-byte files from Azure Media Services to Azure Blob Storage is due to:

  • If the copy operation is aborted before it completes. When you abort a copy operation, the destination blob will have a zero length.

  • The copy operation is not guaranteed to start immediately or complete within a specified time frame and is performed on a best-effort basis.

You can use the code below to copy operation fails and the destination blob has a zero length. If the copy operation succeeds, it prints a message to the console with the size of the destination blob using .net.

Code:

using Azure.Core;
using Azure.Storage.Blobs;

namespace CopyBlobExample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string connectionString = "xxxxx";
            string containerName = "";
            string fileName = "234-ccc23";
            string url = "<your-source-url>";

            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            // Start the copy operation
            var operation = await blobClient.StartCopyFromUriAsync(new Uri(url));

            // Wait for the copy operation to complete
            var fileSize = await operation.WaitForCompletionAsync(
                DelayStrategy.CreateExponentialDelayStrategy(
                    initialDelay: TimeSpan.FromSeconds(1), maxDelay: TimeSpan.FromMinutes(5)),
                CancellationToken.None);

            if (fileSize == 0)
            {
                Console.WriteLine("Copy operation failed. Destination blob has zero length.");
            }
            else
            {
                Console.WriteLine("Copy operation succeeded. Destination blob size: " + fileSize);
            }
        }
    }
}

Output:

Copy operation succeeded. Destination blob size: Status: 200, Value: 157030359

Screenshot of successful copy operation

Also, You can refer this Azure data movement Library to Copy a file from URL to a blob.