如何检查元数据是否为空

How to check if metadata is empty

我创建了一个 addMetadata class 来将元数据添加到存储在 Azure 容器中的 BLOB。但是在我想添加新的元数据之前,我想检查现有的元数据,并且只有在没有元数据的情况下我才想做一些事情。我尝试了以下代码:

 if (properties.Metadata == null)
                    {
                        // Do stuff... 
                    }

其中 properties 是从 BlobClient.GetPropertiesAsync() 生成的 BlobProperties 变量。但这似乎不起作用,有人有任何其他建议吗?

非常感谢!

我在我的系统中试过了

尝试使用 blob.Metadata.Count 如果元数据数据未设置 returns 整数值 returns 0 否则 returns 计数值blob 中的元数据。然后检查计数值是否为 0 添加新的元数据数据

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Connection String");
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("test // container name");
            CloudBlockBlob blob = container.GetBlockBlobReference("caseno.txt //blob name");
            blob.FetchAttributes();
            Console.WriteLine("Blob metadata:");

            // Enumerate the blob's metadata.
            Console.WriteLine(blob.Metadata);
            //Check the blobs metadata 
            if (blob.Metadata.Count == 0)
            {
                blob.Metadata.Add("docType", "textDocuments");

                // Add metadata to the blob by using key/value syntax.
                blob.Metadata["caseno"] = "caseno";
                blob.SetMetadata();
            }
            foreach (var metadataItem in blob.Metadata)
            {
                Console.WriteLine("\tKey: {0}", metadataItem.Key);
                Console.WriteLine("\tValue: {0}", metadataItem.Value);
            }

输出