- Microsoft Windows Azure Development Cookbook
- Neil Mackenzie
- 527字
- 2021-04-02 18:51:58
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:
- Add a new class named
RootContainerExample
to the project. - Set the Target Framework for the project to.NET Framework 4.
- Add the following assembly references to the project:
Microsoft.WindowsAzure.StorageClient System.Configuration
- Add the following
using
statements to the top of the class file:using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient; using System.Configuration;
- 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); }
- Add the following method, using
UploadCrossDomainPolicyFile()
, to the class:public static void UseRootContainerExample() { String crossDomainPolicyFilename = "{PATH_TO_FILE}"; UploadCrossDomainPolicyFile(crossDomainPolicyFilename); }
- Add the following to the
configuration
section ofapp.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.
- Joomla! 1.5 Site Blueprints
- 中文版CorelDRAW 2022基礎教程
- 中文版AutoCAD 2016從入門到精通
- 零基礎玩轉AI繪畫
- 中文版3ds Max 2024完全自學教程
- AutoCAD 2014中文版完全自學手冊
- 中文版Photoshop CS6平面設計實用教程(第2版)
- 無師自通AutoCAD:中文版室內設計
- Linux Shell Scripting Cookbook
- ICEfaces 1.8: Next Generation Enterprise Web Development
- 剪映:從零開始精通短視頻剪輯(電腦版)
- 機械CAD軟件應用入門指導書
- Photoshop海報設計技巧與實戰
- CorelDRAW 2018平面設計基礎教程(第3版)
- Expert Python Programming