3

I'm copying files from Azure file shares to CloudBlockBlob in a storage container. I want to verify that the bytes (.Properties.Length) is the same in both locations before I delete the original. I thought it would be a case of getting a new reference to the copied blob, however it is always -1.

The copy works fine and a visual inspection of the file v blob shows the bytes are identical, just not sure how to replicate this in my C# application.

The line I'm having issues with is the one that defines the "copied" object.

string myfile = @"junk.txt";

CloudFile sourcefile = 
    fileStorage.Share.GetRootDirectoryReference().GetFileReference(myfile);
CloudBlockBlob destBlob = 
     destStorage.Container.GetBlockBlobReference(myfile);
string fileSAS = sourcefile.GetSharedAccessSignature(new 
    SharedAccessFilePolicy()
{
    Permissions = SharedAccessFilePermissions.Read,
    SharedAccessExpiryTime = DateTime.Now.AddHours(24)
});
Uri fileUri = new Uri(sourcefile.StorageUri.PrimaryUri.ToString() + fileSAS);
CloudBlockBlob destBlob = destStorage.Container.GetBlockBlobReference(file.Path);
destBlob.StartCopy(fileUri);
CloudBlockBlob copied = destStorage.Container.GetBlockBlobReference(myfile);
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
Dave
  • 313
  • 1
  • 7
  • 18

1 Answers1

8

Before you want to fetch property / metadata, you need to use the method FetchAttributes() first, which is used to populate properties and metadata.

Please try the code below:

    static void Main(string[] args)
    {
        string myfile = "123.txt";
        CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("account_name", "account_key"), true);

        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
        CloudFileShare fileShare = fileClient.GetShareReference("test");
        CloudFile sourceFile = fileShare.GetRootDirectoryReference().GetFileReference(myfile);

        sourceFile.FetchAttributes();
        Console.WriteLine("The source file length is: "+sourceFile.Properties.Length);

        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference("aa1");
        CloudBlockBlob destBlob = container.GetBlockBlobReference(myfile);

        string fileSAS = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy() {
            Permissions = SharedAccessFilePermissions.Read,
            SharedAccessExpiryTime=DateTime.Now.AddHours(24)
        });

        Uri fileUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSAS);
        Console.WriteLine("--copy started--");
        destBlob.StartCopy(fileUri);            

        destBlob = container.GetBlockBlobReference(myfile);
        destBlob.FetchAttributes();

        //use poll to check if the copy is completed or not.
        while (destBlob.CopyState.Status == CopyStatus.Pending)
        {
            Thread.Sleep(500);
            destBlob.FetchAttributes();
        }

        //when the copy completed, then check the copied file length.
        if (destBlob.CopyState.Status == CopyStatus.Success)
        {
            Console.WriteLine("the dest blob length is: " + destBlob.Properties.Length);
        }
        else
        {
            Console.WriteLine("the copy operation is failed!");
        }


        Console.ReadLine();
    }

Test result as below:

The source file length is: 184227539

--copy started--

the dest blob length is: 184227539

You can also refer to the screenshot for more details.

Text output screenshot

Super Jade
  • 5,609
  • 7
  • 39
  • 61
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • 1
    Note that you'd need to poll the copy state after `destBlob.StartCopy(fileUri);`, since the copy is done on server side asynchronously. Here is a similar question: https://stackoverflow.com/questions/52503777/does-cloudblockblob-startcopyasync-return-when-the-copy-is-completed – Zhaoxing Lu Apr 03 '19 at 04:46
  • 1
    @ZhaoxingLu-Microsoft, thanks for pointing it out! I just thought StartCopy() would be threading block until copy actually finished. Updated my answer with poll the copy state. – Ivan Glasenberg Apr 03 '19 at 05:53
  • The looping also appears to have fixed another issue were the copied blobs were empty, this happened a lot more often with large files. I was going to re-write that part to use asynchronous methods but your code seems to achieve the same result. I did not use asynchronous to begin with as I was only ever going to perform one copy at a time. – Dave Apr 03 '19 at 14:36
  • Could you [please post the output as text rather than a picture of text](https://meta.stackexchange.com/questions/320052/why-are-images-of-text-code-and-mathematical-expressions-discouraged/320060#320060)? Thanks! – Super Jade Apr 04 '19 at 04:18