c# - 使用 blob StartCopyAsync 时如何获取 azure blob 的更新复制状态

标签 c# azure azure-blob-storage

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

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

最佳答案

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/

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

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

关于c# - 使用 blob StartCopyAsync 时如何获取 azure blob 的更新复制状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40965876/

相关文章:

c# - "An assembly with the same simple name has already been imported"没有重复引用的错误

c# - 在c#中将0和1的数组转换为int

asp.net - 将多项目 MVC4 解决方案部署到 Azure 失败

sql - 如何将存储在 Azure Blob 存储中的图像文件插入 Azure SQL 表中

c# - JSON.net:如何在不使用默认构造函数的情况下反序列化?

C# MySQL 从与另一个匹配的列中检索值

c# - Azure Web 角色启动时 IIS Express 失败

python - Azure SQL 数据库的 Sqlalchemy 问题

azure - 使用 JavaScript 将文件放入 Blob 存储会导致 403(服务器无法验证请求)

Azure WebApp 不将应用程序日志发送到 Blob 存储