Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

Azure.Storage.Blobs.BlobServiceClient CopyFromUri() doesn't seem to be finished copying before returning latest ETag

I am trying to use StartCopyFromUri or StartCopyFromUriAsync to copy a blob from one storage account to another. Even though status.HasCompleted when I try to get the ETag either through

1. var etag = await _siteStorageClient.GetBlobETag(containerPath, asset.BlobName);
 //this is the response from WaitForCompletionAsync
2. var etag = complete.GetRawResponse().Headers.Where(x => x.Name == "ETag").FirstOrDefault().Value;

I've tried both methods and both return an Etag that doesn't match what is shown in the properties of the blob when I log in through Azure Portal. It is almost as if the file wasn't done copying(or race condition) when the Etag check was executed. I couldn't find any usage samples on github for the SDK.

Any ideas what could be going awry?

This a similar question but using the older SDK. How to copy a blob from one container to another container using Azure Blob storage SDK

//Storage class
public async Task<CopyFromUriOperation> CopyFile(string containerName, string blobName, Uri sourceUri)
            {
                var container = _blobServiceClient.GetBlobContainerClient(containerName);
                var blockBlobClient = container.GetBlockBlobClient(blobName);
    
                //Made this the synchronous version try and block 
                //this is the target client
                var status =  await blockBlobClient.StartCopyFromUriAsync(sourceUri);
    
                while(!status.HasCompleted)
                {      
                    //Per Documentation this calls UpdateStatusAsync() periodically
                    //until status.HasCompleted is true
                    await status.WaitForCompletionAsync();
                }        
                return status;             
            }


    //Calling Code
    
    var status = await _siteStorageClient.CopyFile(container,BlobName, sasUri);
    var etag = await _siteStorageClient.GetBlobETag(container, BlobName);
question from:https://stackoverflow.com/questions/65860383/azure-storage-blobs-blobserviceclient-copyfromuri-doesnt-seem-to-be-finished

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I was able to get this working after a few tries and troubleshooting. It would only happen in the Azure environment and not when running the web app locally.

Initially the status.WaitForCompletionAsync() was inside the loop and I started getting a socket error. I believe it was getting called too many times and was causing port exhaustion(just speculation at this point).

But this is what is working now.

public async Task<CopyFromUriOperation> CopyFile(string containerName, string blobName,Uri sourceUri)
        {
            var container = _blobServiceClient.GetBlobContainerClient(containerName);
            var blockBlobClient = container.GetBlockBlobClient(blobName);

            var status =  await blockBlobClient.StartCopyFromUriAsync(sourceUri);

            await status.WaitForCompletionAsync();

            while(status.HasCompleted == false)
            {
                await Task.Delay(100);          
            }

            return status;
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...