且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Blob StartCopyAsync时如何获取Azure Blob的更新副本状态

更新时间:2021-07-04 23:53:56

轮询副本Blob"属性:现在,我们提供以下附加属性,这些属性允许用户使用获取Blob x-ms-copy-status(或CopyStatus):复制操作的当前状态.可以是以下之一:未决:拷贝操作未决.成功:复制操作成功完成.已中止:客户端中止了复制操作.失败:由于错误,复制操作无法完成.

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):复制操作返回的ID,可用于监视进度或中止复制.

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):可用于诊断的其他错误信息.

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

x-ms-copy-progress(CopyProgress):到目前为止已复制的Blob数量.格式为X/Y,其中X =复制的字节数,Y为总字节数.

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):最后一个副本的完成时间.

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

可以监视这些属性,以跟踪返回待处理"状态的复制操作的进度.但是,需要注意的是,除了放置页面",放置块"和租赁Blob"操作外,目标Blob上的任何其他写入操作(即放置Blob",放置块列表",设置Blob元数据",设置Blob属性")都将删除这些属性.与复制操作有关.

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/

请注意,您需要定期从Azure存储服务器端轮询复制状态, await Task.Delay(TimeSpan.FromSeconds(10)); 实际上不执行任何操作.

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);
    };
}