How to get updated copy state of azure blob when using blob StartCopyAsync

我与影子孤独终老i 提交于 2019-12-11 08:17:33

问题


I have some c# code to copy a blob from one storage account to another. I noticed that when I make the call to CloudBlob.StartCopyAsync, the target blob's CopyState.Status is set to CopyStatus.Pending. Is there any way that I can get an updated status on the copy operation?

I have tried adding a await Task.Delay(TimeSpan.FromSeconds(10)); after the call, but when the delay finishes, the state still shows pending. If I then try to re-obtain the blob from the storage container, I get CopyStatus == null.


回答1:


Polling for Copy Blob properties: we now provide the following additional properties that allow users to track the progress of the copy, using Get Blob Properties, Get Blob, or List Blobs:

x-ms-copy-status (or CopyStatus): The current status of the copy operation. It can be one of the following: pending: Copy operation is pending. success: Copy operation completed successfully. aborted: Copy operation was aborted by a client. failed: Copy operation failed to complete due to an error.

x-ms-copy-id (CopyId): The id returned by the copy operation which can be used to monitor the progress or abort a copy.

x-ms-copy-status-description (CopyStatusDescription): Additional error information that can be used for diagnostics.

x-ms-copy-progress (CopyProgress): The amount of the blob copied so far. This has the format X/Y where X=number of bytes copied and Y is the total number of bytes.

x-ms-copy-completion-time (CopyCompletionTime): The completion time of the last copy.

These properties can be monitored to track the progress of a copy operation that returns “pending” status. However, it is important to note that except for Put Page, Put Block and Lease Blob operations, any other write operation (i.e., Put Blob, Put Block List, Set Blob Metadata, Set Blob Properties) on the destination blob will remove the properties pertaining to the copy operation.

https://blogs.msdn.microsoft.com/windowsazurestorage/2012/06/12/introducing-asynchronous-cross-account-copy-blob/

Please note that you need to periodically poll the copy state from Azure Storage server side, await Task.Delay(TimeSpan.FromSeconds(10)); does nothing actually.

public static void MonitorCopy(CloudBlobContainer destContainer)
{
    bool pendingCopy = true;

    while (pendingCopy)
    {
        pendingCopy = false;
        var destBlobList = destContainer.ListBlobs(
            true, BlobListingDetails.Copy);

        foreach (var dest in destBlobList)
        {
            var destBlob = dest as CloudBlob;

            if (destBlob.CopyState.Status == CopyStatus.Aborted ||
                destBlob.CopyState.Status == CopyStatus.Failed)
            {
                // Log the copy status description for diagnostics 
                // and restart copy
                Log(destBlob.CopyState);
                pendingCopy = true;
                destBlob.StartCopyFromBlob(destBlob.CopyState.Source);
            }
            else if (destBlob.CopyState.Status == CopyStatus.Pending)
            {
                // We need to continue waiting for this pending copy
                // However, let us log copy state for diagnostics
                Log(destBlob.CopyState);

                pendingCopy = true;
            }
            // else we completed this pending copy
        }

        Thread.Sleep(waitTime);
    };
}


来源:https://stackoverflow.com/questions/40965876/how-to-get-updated-copy-state-of-azure-blob-when-using-blob-startcopyasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!