导出 (0) 打印
全部展开

设置并检索属性和元数据

更新时间: 2014年11月

除本身包含的数据外,容器和 Blob 还支持两种形式的关联数据:

  • 系统属性。系统属性存在于每个容器或 Blob 资源上。其中一些属性是可以读取或设置的,而另一些属性是只读的。事实上,有些系统属性与 Windows Azure 托管库为您保留的某些标准 HTTP 标头对应。

  • 用户定义的元数据。用户定义的元数据是在给定资源上以名称/值对的形式指定的元数据。您可以使用元数据来存储容器或 Blob 的其他值;这些值仅用于您个人目的,不会影响容器和 Blob 的行为方式。

Important重要
检索资源的属性和元数据值的过程分为两步。您必须先在您的 CloudBlobContainerCloudBlockBlobCloudPageBlob 对象上显式获取这些值,然后才能读取它们。若要以同步方式获取属性和元数据,请对容器或 Blob 调用 FetchAttributes;若要以异步方式获取它们,请调用 BeginFetchAttributesEndFetchAttributes

设置和检索属性

容器只有只读属性,而 Blob 同时具有只读属性和读写属性。若要在 Blob 上设置属性,请指定属性值,然后调用 SetProperties 方法或 SetProperties 方法。

若要读取容器或 Blob 的属性,请调用 FetchAttributes 方法,然后检索属性值。

以下代码示例创建容器和 Blob 并将属性值写入到控制台窗口。此示例使用了存储仿真程序,因此必须运行模拟的存储服务,这样代码才能工作:


// Use the storage emulator account.
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;

// As an alternative, you can create the credentials from the account name and key.
// string accountName = "myaccount";
// string accountKey = "SzlFqgzqhfkj594cFoveYqCuvo8v9EESAnOLcTBeBIo31p16rJJRZx/5vU/oY3ZsK/VdFNaVpm6G8YSD2K48Nw==";
// StorageCredentials credentials = new StorageCredentials(accountName, accountKey);
// CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve a reference to a container. 
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Create the container if it does not already exist.
container.CreateIfNotExists();

// Fetch container properties and write out their values.
container.FetchAttributes();
Console.WriteLine("Properties for container " + container.Uri + ":");
Console.WriteLine("LastModifiedUTC: " + container.Properties.LastModified);
Console.WriteLine("ETag: " + container.Properties.ETag);
Console.WriteLine();

// Create a blob.
CloudBlockBlob blob = container.GetBlockBlobReference("myblob.txt");

// Create or overwrite the "myblob.txt" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"c:\test\myblob.txt"))
{
   blob.UploadFromStream(fileStream);
} 

// Fetch container properties and write out their values.
container.FetchAttributes();
Console.WriteLine("Properties for container " + container.Uri + ":");
Console.WriteLine("LastModifiedUTC: " + container.Properties.LastModified);
Console.WriteLine("ETag: " + container.Properties.ETag);
Console.WriteLine();

// Create a blob.
Uri blobUri =new UriBuilder(containerUri.AbsoluteUri + "/ablob.txt").Uri;
CloudPageBlob blob = new CloudPageBlob(blobUri, credentials);
blob.Create(1024);
            

// Set the CacheControl property.
blob.Properties.CacheControl = "public, max-age=31536000";
blob.SetProperties();

// Fetch blob attributes.
blob.FetchAttributes();

Console.WriteLine("Read-only properties for blob " + blob.Uri + ":");
Console.WriteLine("BlobType: " + blob.Properties.BlobType);
Console.WriteLine("ETag: " + blob.Properties.ETag);
Console.WriteLine("LastModifiedUtc: " + blob.Properties.LastModified);
Console.WriteLine("Length: " + blob.Properties.Length);
Console.WriteLine();

Console.WriteLine("Read-write properties for blob " + blob.Uri + ":");
Console.WriteLine("CacheControl: " +
   (blob.Properties.CacheControl == null ? "Not set" : blob.Properties.CacheControl));
Console.WriteLine("ContentEncoding: " +
   (blob.Properties.ContentEncoding == null ? "Not set" : blob.Properties.ContentEncoding));
Console.WriteLine("ContentLanguage: " +
   (blob.Properties.ContentLanguage == null ? "Not set" : blob.Properties.ContentLanguage));
Console.WriteLine("ContentMD5: " +
   (blob.Properties.ContentMD5 == null ? "Not set" : blob.Properties.ContentMD5));
Console.WriteLine("ContentType: " +
   (blob.Properties.ContentType == null ? "Not set" : blob.Properties.ContentType));

// Clean up.
blob.DeleteIfExists();
container.Delete();

设置和检索元数据

您可以在 Blob 或容器资源上指定元数据作为一个或多个名称/值对。若要设置元数据,请将名称/值对添加到资源上的 Metadata 集合,然后调用 SetMetadata 方法以将值保存到服务。

note注释
元数据的名称必须符合 C# 标识符命名约定。

若要检索元数据,请对 Blob 或容器调用 FetchAttributes 方法以填充 Metadata 集合,然后读取值。

以下代码示例创建新容器并为其设置元数据,然后读回元数据值:



// Account name and key.  Modify for your account.
string accountName = "myaccount";
string accountKey = "SzlFqgzqhfkj594cFoveYqCuvo8v9EESAnOLcTBeBIo31p16rJJRZx/5vU/oY3ZsK/VdFNaVpm6G8YSD2K48Nw==";

// Get a reference to the storage account and client with authentication credentials.
StorageCredentials credentials = new StorageCredentials(accountName, accountKey);
CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve a reference to a container. 
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Create the container if it does not already exist.
container.CreateIfNotExists();

// Set metadata for the container.
container.Metadata["category"] = "images";
container.Metadata["owner"] = "azure";
container.SetMetadata();

// Get container metadata.
container.FetchAttributes();
foreach (string key in container.Metadata.Keys)
{
   Console.WriteLine("Metadata key: " + key);
   Console.WriteLine("Metadata value: " + container.Metadata[key]);
}

//Clean up.
container.Delete();

另请参阅

显示:
© 2014 Microsoft