使用Blob StartCopyAsync时如何获取Azure Blob的更新副本状态 [英] How to get updated copy state of azure blob when using blob StartCopyAsync

查看:57
本文介绍了使用Blob StartCopyAsync时如何获取Azure Blob的更新副本状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些C#代码可以将一个存储帐户中的Blob复制到另一个存储帐户中.我注意到,当我调用 CloudBlob.StartCopyAsync 时,目标Blob的 CopyState.Status 被设置为 CopyStatus.Pending .有什么方法可以使我获得复制操作的最新状态?

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?

我尝试在调用后添加 await Task.Delay(TimeSpan.FromSeconds(10)); ,但是当延迟结束时,状态仍显示为待处理.如果然后尝试从存储容器中重新获取Blob,则会得到 CopyStatus == null .

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.

推荐答案

轮询副本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);
    };
}

这篇关于使用Blob StartCopyAsync时如何获取Azure Blob的更新副本状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆