如何获得刚刚复制到Azure存储帐户的CloudBlockBlob的大小?

时间:2019-04-02 19:33:08

标签: azure-storage azure-storage-blobs azure-files

我正在将文件从Azure文件共享复制到存储容器中的CloudBlockBlob。在删除原始字节之前,我想验证两个位置的字节(.Properties.Length)是否相同。我以为是要获得对复制的Blob的新引用的情况,但是始终为-1。

该副本工作正常,并且对文件v blob进行了目视检查,显示字节相同,只是不确定如何在我的C#应用​​程序中复制此字节。

我遇到问题的那一行是定义“复制”对象的那一行。

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);

1 个答案:

答案 0 :(得分:1)

要获取属性/元数据之前,首先需要使用方法FetchAttributes(),该方法用于填充属性和元数据。

请尝试以下代码:

    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();
    }

测试结果如下:

源文件长度为:184227539

-开始复制-

目标blob长度是:184227539

您也可以参考屏幕截图以获取更多详细信息。

Text output screenshot

相关问题