阅读背景:

即使我调用CloudBlob.SetMetadata,也不会保存Blob元数据

来源:互联网 

For a few hours I've been trying to set some metadata on the blob I create using the Azure SDK. I upload the data asynchronously using BeginUploadFromStream() and everything works smoothly. I can access the blob using its URI when the upload has completed, so it is created successfully, however any metadata I set is not persisted.

几个小时以来,我一直在尝试使用Azure SDK在我创建的blob上设置一些元数据。我使用BeginUploadFromStream()异步上传数据,一切顺利。我可以在上传完成后使用其URI访问blob,因此创建成功,但我设置的任何元数据都不会保留。

I set the metadata after calling EndUploadFromStream().

我在调用EndUploadFromStream()之后设置了元数据。

I've tried setting the metadata the three ways I can find through the documentation:

我尝试通过文档找到三种方式来设置元数据:

// First attempt
myBlob.Metadata["foo"] = "bar";

// Second attempt
myBlob.Metadata.Add("foo", "bar");

//Third attempt
var metadata = new NameValueCollection();
metadata["foo"] = "bar";
blob.Metadata.Add(metadata);

After setting the metadata i call myBlob.SetMetadata() to save the metadata to Azure, as specified by the documentation, but it does not stick. The call doesn't trow any exceptions, but when I get a new reference to my blob, it doesn't have any metadata.

设置元数据后,我调用myBlob.SetMetadata()将元数据保存到Azure,如文档所指定,但它不会粘。该调用不会引发任何异常,但是当我获得对我的blob的新引用时,它没有任何元数据。

I've tried saving the metadata asynchronously as well using BeginSetMetadata() and EndSetMetadata() but with similar result.

我尝试使用BeginSetMetadata()和EndSetMetadata()异步保存元数据,但结果相似。

I start to think I'm missing something really trivial here, but after staring at it for five hours, I still cannot understand where I go wrong?

我开始认为我在这里遗漏了一些非常微不足道的东西,但是在盯着它看了五个小时后,我仍然无法理解我哪里出错了?

1 个解决方案

#1


21  

SetMetadata should work as expected. But simply getting a reference to the blob isn't sufficient to read the metadata.

SetMetadata应该按预期工作。但仅仅获取blob的引用并不足以读取元数据。

After getting the blob reference, you need to call the FetchAttributes method on that CloudBlob. This will load all properties and metadata, and only then will you be able to access the metadata you set previously:

获取blob引用后,需要在该CloudBlob上调用FetchAttributes方法。这将加载所有属性和元数据,然后您才能访问先前设置的元数据:

// Get a reference to a blob.
CloudBlob blob = blobClient.GetBlobReference("mycontainer/myblob.txt");

// Populate the blob's attributes.
blob.FetchAttributes();

// Enumerate the blob's metadata.
foreach (var metadataKey in blob.Metadata.Keys)
{
    Console.WriteLine("Metadata name: " + metadataKey.ToString());
    Console.WriteLine("Metadata value: " + blob.Metadata.Get(metadataKey.ToString()));
}

分享到: