2012-06-18 46 views
5

有人可以告訴我,爲什麼我們不能在一個容器內的容器內創建一個容器在天藍色的存儲?還有任何需要處理的方法,我們需要在Azure存儲中創建目錄層次結構?天青容器

回答

14

您不能在容器中創建容器,因爲Windows Azure根本不支持容器容器(您應該將容器看作「磁盤驅動器」,就像您的磁盤驅動器C:\ disk)。但是通過CloudBlobDirectory類支持使用目錄。下面是Neil's blog一個例子:

protected void GetDirectoryList(String topLevelDirectoryName, String subDirectoryName) 
{ 
    CloudStorageAccount cloudStorageAccount = 
     CloudStorageAccount.FromConfigurationSetting(「DataConnectionString」); 
    CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); 

    CloudBlobDirectory topLevelDirectory = cloudBlobClient.GetBlobDirectoryReferencetopLevelDirectoryName); 

    CloudBlobDirectory subDirectory = topLevelDirectory.GetSubdirectory(subDirectoryName); 

    IEnumerable<IListBlobItem> blobItems = subDirectory.ListBlobs(); 
    foreach (IListBlobItem blobItem in blobItems) 
    { 
     Uri uri = blobItem.Uri; 
    } 
} 
+1

在此值得一提的是,類似文件夾的結構是通過斑點名稱中使用反斜線(\)來實現的。因此,我們可以使用CloudBlobDirectories,它只是一個具有相同前綴的斑點列表(即images \ someblob.jpg,images \ otherblob.jpg)。而單個blob的全名是「images \ someblob.jpg」) – astaykov

+3

對@ astaykov的評論的修正:使用了正斜槓(/),這只是默認設置。該API可讓您指定所需的任何分隔符。 – smarx

+1

目前在CloudBlobClient中沒有GetBlobDirectoryReference函數。要列出目錄的內容,你可以使用這個答案http://stackoverflow.com/a/25312000/1519458 –

1

下面是從我的課蔚藍的容器中的工作代碼(用於管理BLOB)的一個工作項目。
PLS和PLS注意,您在容器中的文件夾,並在容器中的BLOB文件的命名應該是小寫字母或者你可能有一個錯誤

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.IO; 
using System.Linq; 
using System.Web; 
using Microsoft.WindowsAzure; 
using Microsoft.WindowsAzure.Storage; 
using Microsoft.WindowsAzure.Storage.Auth; 
using Microsoft.WindowsAzure.Storage.Blob; 

    namespace XXXXXXXXXXX 
{ 

public class Blob 
{ 

    private CloudBlobContainer Prerequisite(string userId) 
    { 
     var con = ConfigurationManager.AppSettings["StorageConnectionString"]; 
     // Retrieve storage account from connection string. 
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
      con); 

     // Create the blob client. 
     CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

     // Retrieve reference to a previously created container. 
     CloudBlobContainer container = blobClient.GetContainerReference(userId); 

     return container; 
    } 


    public void CreateUserContainerIfNotExisting(string userId) 
    { 
     CloudBlobContainer container = Prerequisite(userId); 

     // Create the container if it doesn't already exist. 
     container.CreateIfNotExists(); 

     //Public access to all items in the container (meaning public can see it and download it but not modify and delete it) 
     container.SetPermissions(
      new BlobContainerPermissions 
      { 
       PublicAccess = 
        BlobContainerPublicAccessType.Blob 
      }); 
    } 

    public void ReadFileInBlob(string userId) 
    { 
     CloudBlobContainer container = Prerequisite(userId); 

     // Loop over items within the container and output the length and URI. 
     // foreach (IListBlobItem item in container.ListBlobs(null, false)) 
     foreach (IListBlobItem item in container.ListBlobs(null, true)) 
     { 
      if (item.GetType() == typeof(CloudBlockBlob)) 
      { 
       CloudBlockBlob blob = (CloudBlockBlob)item; 

       Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri); 

      } 
      else if (item.GetType() == typeof(CloudPageBlob)) 
      { 
       CloudPageBlob pageBlob = (CloudPageBlob)item; 

       Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri); 

      } 
      else if (item.GetType() == typeof(CloudBlobDirectory)) 
      { 
       CloudBlobDirectory directory = (CloudBlobDirectory)item; 

       Console.WriteLine("Directory: {0}", directory.Uri); 
      } 
     } 
    } 

    public CloudBlockBlob AddOrModifyItemToBlob(string userId, string itemKey) 
    { 
     CloudBlobContainer container = Prerequisite(userId); 

     // Retrieve reference to a blob named "myblob". 
     CloudBlockBlob blockBlob = container.GetBlockBlobReference(itemKey); 

     return blockBlob; 

    } 

    public void DownloadToFolderLocation(string userId, string itemKey, string location) 
    { 
     CloudBlobContainer container = Prerequisite(userId); 

     // Retrieve reference to a blob named "photo1.jpg". 
     CloudBlockBlob blockBlob = container.GetBlockBlobReference(itemKey); 

     // Save blob contents to a file. 
     using (var fileStream = System.IO.File.OpenWrite(location)) 
     { 
      blockBlob.DownloadToStream(fileStream); 
     } 
    } 

    public string DownloadAsStream(string userId, string itemKey) 
    { 
     CloudBlobContainer container = Prerequisite(userId); 

     // Retrieve reference to a blob named "myblob.txt" 
     CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(itemKey); 

     string text; 
     using (var memoryStream = new MemoryStream()) 
     { 
      blockBlob2.DownloadToStream(memoryStream); 
      text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()); 
     } 
     return text; 
    } 

    public void DeleteBlobFile(string userId, string itemKey) 
    { 
     CloudBlobContainer container = Prerequisite(userId); 

     // Retrieve reference to a blob named "myblob.txt". 
     CloudBlockBlob blockBlob = container.GetBlockBlobReference(itemKey); 

     // Delete the blob. 
     blockBlob.Delete(); 
    } 

    } 
    } 
1

到另一個容器內創建一個容器的最簡單方法對於Azure blob存儲,則是使用來自Cerebrata的稱爲Azure Explorer的免費Azure存儲管理工具。

它允許您通過創建一個新文件夾來創建容器。

Azure Explorer