mctr
mctr

Reputation: 51

Why CloudBlockBlob.StartCopyAsync sometimes writes only 0 bytes?

I have a case that sometimes CloudBlockBlob.StartCopyAsync writes only 0 bytes data into the input asset's blob... What should be the case? So, I decided to start copy while block blob length will not be increased... Any suggestions?

private async Task CreateInputAssetBlobAsync(UploadRequest request)
{
    var cloudBlobContainer = new CloudBlobContainer(request.InputAssetStorageUri);
    var blockBlob = cloudBlobContainer.GetBlockBlobReference(request.BlobName);

    var storageCredentials = new Microsoft.Azure.Storage.Auth.StorageCredentials(_apiAccess.TempBlobAccountName, _apiAccess.TempBlobContainerKey);
    var tempBlobContainer = new CloudBlobContainer(new Uri(_apiAccess.TempBlobContainerAddress), storageCredentials);
    var tempBlockBlob = tempBlobContainer.GetBlockBlobReference(request.BlobName);

    try
    {
        await blockBlob.StartCopyAsync(tempBlockBlob);

        do
        {
            if (blockBlob.CopyState.Status == CopyStatus.Pending)
                await Task.Delay(1000);
            await blockBlob.FetchAttributesAsync();
        }
        while (blockBlob.CopyState.Status != CopyStatus.Success);

        return;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}

Upvotes: 0

Views: 631

Answers (1)

Jason Pan
Jason Pan

Reputation: 21883

You should use below code when you want to copy the blob content.

For more detail, you can download my sample in github(BlobContainerCopy). It works, and I think it useful to you.

You also can refer to this post. Hope my answer in this post can also help you.

Error during storage blob copy operation - The requested operation is not allowed in the current state of the entity

foreach (ICloudBlob sourceBlobRef in sourceBlobRefs)
{
    if (sourceBlobRef.Properties.ContentType != null)
    {
        // Copy the source blob
        CloudBlockBlob destBlob = destContainer.GetBlockBlobReference(sourceBlobRef.Name);

        try
        {
            //exception throwed here  - StartCopyAsync
            await destBlob.StartCopyAsync(new Uri(GetSharedAccessUri(sourceBlobRef.Name, sourceContainer)));

            ICloudBlob destBlobRef = await destContainer.GetBlobReferenceFromServerAsync(sourceBlobRef.Name);
            while (destBlobRef.CopyState.Status == CopyStatus.Pending)
            {
                Console.WriteLine($"Blob: {destBlobRef.Name}, Copied: {destBlobRef.CopyState.BytesCopied ?? 0} of  {destBlobRef.CopyState.TotalBytes ?? 0}");
                await Task.Delay(500);
                destBlobRef = await destContainer.GetBlobReferenceFromServerAsync(sourceBlobRef.Name);
            }
            Console.WriteLine($"Blob: {destBlob.Name} Complete");
        }
        catch (Exception e)
        {
            Console.WriteLine($"Blob: {destBlob.Name} Copy Failed");
        }
    }
}

Upvotes: 2

Related Questions