Reputation: 170499
Earlier versions of Azure SDK had CloudBlob.CopyFromBlob()
which was synchronous and which we use in our code. We now need to move to SDK 2.4 and there's no such method there, instead there's CloudBlockBlob.StartCopyFromBlob()
that returns some magic token which we can use to check how the copy is being done.
There's no point in "background" copying of blobs in our code - doing the copy synchronously would be just fine, so it'd be nice to somehow implement an equivalent of old CopyFromBlob()
using the stuff from the newer SDK.
Here's my approach:
CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(sourcePath);
CloudBlockBlob targetBlob = targetContainer.GetBlockBlobReference(targetPath);
var copyToken = targetBlob.StartCopyFromBlob(sourceBlob.Uri);
while( true ) {
System.Threading.Thread.Sleep(100);
CloudBlockBlob target = targetContainer.GetBlockBlobReference(targetPath);
bool greatSuccess = false;
switch( target.CopyState.Status ) {
case CopyStatus.Success:
greatSuccess = true;
break;
case CopyStatus.Pending:
continue;
default:
throw new Exception( "Failed to copy" );
}
if( success ) {
break;
}
}
It looks working, however I'm not sure there're no potential problems in there.
What's the idiomatic way of using StartCopyFromBlob()
and then waiting till the copy is complete?
Upvotes: 3
Views: 4372
Reputation: 332
If the copy operation is for the same storage account and the same type(example -> block blob to block blob copy in the same storage account), copy is synchronous and you can expect that the copy operation has completed when StartCopyFromBlob() returns. You can also validate it by checking targetBlob.CopySate value to be success.
CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(sourcePath);
CloudBlockBlob targetBlob = targetContainer.GetBlockBlobReference(targetPath);
var copyToken = targetBlob.StartCopyFromBlob(sourceBlob.Uri);
if(targetBlob.CopyState.Status == CopyStatus.Success)
{
// Copy completed successfully
}
For all other scenarios (across storage accounts or even across types within the same storage account) copy operation is asynchronous and uses spare resources in the background to execute. Currently, service does not give an SLA on how fast it can copy over. If you want consistent behavior in those cases, best option is for the client to do it's own copy - download data from source and upload to destination.
Upvotes: 12