官术网_书友最值得收藏!

Creating and using the root container for blobs

The Windows Azure Blob Service supports a simple two-level hierarchy for blobs. There is a single level of containers, each of which may contain zero or more blobs. Containers may not contain other containers.

In the Blob service, a blob resource is addressed as follows:

http://{account}.blob.core.windows.net/{container}/{blob}

{account}, {container}, and {blob} represent the name of the storage account, container, and blob.

This addressing convention works for most uses of blobs. However, when using Silverlight the runtime requires that a cross-domain policy file reside at the root of the domain and not beneath a container, as would be the case with the standard addressing for blobs. The cross-domain policy file allows a web client to access data from more than one domain at a time. (http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx) Microsoft added support for a root container, named $root, to the Blob service, so that it could host cross-domain policy files.

The root container name does not need to be provided when retrieving blobs contained in it. For example, the following is a valid address for a blob named crossdomain.xml stored in the root container:

http://{account}.blob.core.windows.net/crossdomain.xml

The Silverlight runtime is able to access this blob and use it as a cross-domain policy file.

Note that the names of root-container blobs must not contain the / symbol to avoid any confusion with blobs being named to simulate a directory tree.

In this recipe, we will learn how to create and use the root container for a storage account.

Getting ready

We need to create a file that we will upload using the recipe. As we do not rely on this file being a cross-domain policy file, we can actually use any file for the recipe.

How to do it...

We are going to create a root container and upload a cross-domain policy file to it. We do this as follows:

  1. Add a new class named RootContainerExample to the project.
  2. Set the Target Framework for the project to.NET Framework 4.
  3. Add the following assembly references to the project:
    Microsoft.WindowsAzure.StorageClient
    System.Configuration
  4. Add the following using statements to the top of the class file:
    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;
    using System.Configuration;
  5. Add the following method, uploading the cross-domain policy file, to the class:
    private static void UploadCrossDomainPolicyFile(String fileName)
    {
      String rootContainerName = "$root";
      String crossDomainPolicyName = "crossdomain.xml";
      CloudStorageAccount cloudStorageAccount =CloudStorageAccount.Parse(ConfigurationManager.AppSettings["DataConnectionString"]);
    
      CloudBlobClient cloudBlobClient =cloudStorageAccount.CreateCloudBlobClient();
      CloudBlobContainer cloudBlobContainer =cloudBlobClient.GetContainerReference(rootContainerName);
      cloudBlobContainer.CreateIfNotExist();
      CloudBlockBlob cloudBlockBlob =cloudBlobContainer.GetBlockBlobReference(crossDomainPolicyName);
      cloudBlockBlob.UploadFile(fileName);
    }
  6. Add the following method, using UploadCrossDomainPolicyFile(), to the class:
    public static void UseRootContainerExample()
    {
      String crossDomainPolicyFilename = "{PATH_TO_FILE}";
      UploadCrossDomainPolicyFile(crossDomainPolicyFilename);
    }
  7. Add the following to the configuration section of app.config:
    <appSettings>
      <add key="DataConnectionString"value="UseDevelopmentStorage=true"/>
    </appSettings>

How it works...

In steps 1 through 4, we set up the recipe.

In step 5, we create the root container, with the special name $root, and then use UploadFile() to upload the cross-domain policy file into it.

In step 6, we invoke the UploadCrossDomainPolicyFile() method we added in step 4. We must replace {PATH_TO_FILE} with the actual path to the file.

In step 7, we add the connection string to the app.config configuration file.

See also

  • In the Using blob directories recipe in this chapter, we see how to simulate a directory hierarchy for blobs inside a container.
主站蜘蛛池模板: 肇州县| 广丰县| 咸宁市| 宜州市| 娄底市| 德钦县| 浙江省| 吴堡县| 临猗县| 文成县| 兰州市| 壶关县| 广昌县| 会同县| 汝城县| 北流市| 襄垣县| 崇左市| 华宁县| 武安市| 平昌县| 曲周县| 依兰县| 读书| 渝北区| 舒城县| 陵川县| 延长县| 策勒县| 朔州市| 德格县| 万年县| 库车县| 延津县| 来安县| 乃东县| 侯马市| 大渡口区| 静安区| 凤山市| 淮南市|