我有一些 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.
请注意,您需要定期从 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/