【问题标题】:How do I get the size of an CloudBlockBlob I just copied to Azure storage account?如何获取刚刚复制到 Azure 存储帐户的 CloudBlockBlob 的大小?
【发布时间】:2019-08-24 05:19:35
【问题描述】:

我正在将文件从 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);

【问题讨论】:

    标签: azure azure-storage azure-blob-storage azure-files


    【解决方案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

    --复制开始--

    dest blob 长度为:184227539

    您也可以参考截图了解更多详情。

    【讨论】:

    • 请注意,您需要在destBlob.StartCopy(fileUri); 之后轮询复制状态,因为复制是在服务器端异步完成的。这是一个类似的问题:stackoverflow.com/questions/52503777/…
    • @ZhaoxingLu-Microsoft,谢谢指出!我只是认为 StartCopy() 将成为线程块,直到复制实际完成。通过轮询复制状态更新了我的答案。
    • 循环似乎还解决了另一个问题,即复制的 blob 为空,这种情况在大文件中更常见。我打算重写那部分以使用异步方法,但您的代码似乎达到了相同的结果。我一开始没有使用异步,因为我一次只执行一个副本。
    猜你喜欢
    • 2015-02-12
    • 2012-05-04
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多