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

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.
主站蜘蛛池模板: 镇赉县| 神农架林区| 江口县| 荃湾区| 嵊州市| 喀什市| 建始县| 文山县| 北安市| 新兴县| 汾西县| 绥宁县| 双流县| 南江县| 兴宁市| 长沙县| 武冈市| 江孜县| 格尔木市| 孝感市| 湘潭县| 南岸区| 象州县| 台南县| 克拉玛依市| 河间市| 桦甸市| 榆树市| 丹凤县| 个旧市| 安达市| 华池县| 盐亭县| 凤台县| 清丰县| 长葛市| 五峰| 沾化县| 高邑县| 武隆县| 平遥县|