使用 blob StartCopyAsync 时如何获取 azure blob 的更新副本状态
How to get updated copy state of azure blob when using blob StartCopyAsync
我有一些 c# 代码可以将 blob 从一个存储帐户复制到另一个存储帐户。我注意到当我调用 CloudBlob.StartCopyAsync
时,目标 blob 的 CopyState.Status
设置为 CopyStatus.Pending
。有什么方法可以获得复制操作的更新状态?
我试过在调用后加一个await Task.Delay(TimeSpan.FromSeconds(10));
,但是延迟结束后状态还是pending。如果我随后尝试从存储容器中重新获取 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 从一个存储帐户复制到另一个存储帐户。我注意到当我调用 CloudBlob.StartCopyAsync
时,目标 blob 的 CopyState.Status
设置为 CopyStatus.Pending
。有什么方法可以获得复制操作的更新状态?
我试过在调用后加一个await Task.Delay(TimeSpan.FromSeconds(10));
,但是延迟结束后状态还是pending。如果我随后尝试从存储容器中重新获取 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);
};
}