2012-06-18 27 views
5

Ai đó có thể cho tôi biết, tại sao chúng ta không thể tạo một container bên trong một container trong kho lưu trữ xanh? Và bất kỳ phương pháp để xử lý, nơi chúng tôi cần phải tạo ra hệ thống phân cấp thư mục trong lưu trữ Azure?Azure Containers

Trả lời

14

Bạn không thể tạo vùng chứa trong vùng chứa vì Windows Azure đơn giản không hỗ trợ vùng chứa thừa kế (bạn sẽ thấy vùng chứa dưới dạng 'ổ đĩa' như bạn đĩa C: \). Nhưng làm việc với các thư mục được hỗ trợ thông qua lớp CloudBlobDirectory. Dưới đây là ví dụ từ 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

Điều đáng nói ở đây là cấu trúc giống thư mục đạt được thông qua sử dụng dấu gạch chéo ngược (\) trong tên Blob. Vì vậy, chúng tôi có thể có CloudBlobDirectories, đây chỉ là một danh sách các đốm màu có cùng tiền tố (ví dụ: images \ someblob.jpg, images \ otherblob.jpg). Trong khi tên đầy đủ của một đốm màu duy nhất là "images \ someblob.jpg") – astaykov

+3

Chỉnh sửa nhận xét của @ astaykov: dấu gạch chéo tiến (/) được sử dụng và đây chỉ là giá trị mặc định. API cho phép bạn chỉ định bất kỳ dấu phân cách nào bạn muốn. – smarx

+1

Hiện tại không có chức năng GetBlobDirectoryReference trong CloudBlobClient. Để liệt kê nội dung của một thư mục, bạn có thể sử dụng câu trả lời này http://stackoverflow.com/a/25312000/1519458 –

1

Dưới đây là mã làm việc cho thùng chứa azure của tôi (được sử dụng để quản lý blob) trong dự án đang hoạt động.
Xin vui lòng và xin lưu ý rằng đặt tên lại các thư mục trong container và các tập tin blob trong container phải ở trong dạng chữ thường hay bạn có thể có một lỗi

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

Cách đơn giản nhất để tạo một container trong container khác để lưu trữ Azure blob là sử dụng công cụ quản lý lưu trữ Azure miễn phí có tên Azure Explorer từ Cerebrata.

Nó cho phép bạn tạo vùng chứa bằng cách tạo thư mục mới.

Azure Explorer