有人可以告訴我,爲什麼我們不能在一個容器內的容器內創建一個容器在天藍色的存儲?還有任何需要處理的方法,我們需要在Azure存儲中創建目錄層次結構?天青容器
Q
天青容器
5
A
回答
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
下面是從我的課蔚藍的容器中的工作代碼(用於管理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存儲管理工具。
它允許您通過創建一個新文件夾來創建容器。
相關問題
- 1. 天青搜索索引html內容
- 2. 錯誤天青
- 3. 在天青
- 4. 天青:在雲
- 5. 天青FileStorage CloudFile
- 6. 天青Webjob System.IO.PathTooLongException
- 7. 刀天青
- 8. MVC 3天青
- 9. 天青創建
- 10. 運行容器的天青數量是多少?
- 11. 天青數據工廠從容器遞歸複製
- 12. 如何獲得設立FQDN爲天青容器實例
- 13. 轉碼的視頻在天青AVI容器(YUV420格式)
- 14. 如何從天青容器中獲取文件夾列表?
- 15. 天青CosmosDB表API
- 16. 天青StorageClient版本
- 17. 天青 - Blob存儲
- 18. Trace.WriteLine在ASP.NET天青
- 19. 什麼是天青
- 20. 發佈服務器項目天青
- 21. 發佈項目(ASP.NET)天青,仍顯示標準天青頁
- 22. GitHub的部署天青
- 23. 天青 - 從到位桶
- 24. 天青下載node.js超時
- 25. 天青Webjob無法運行
- 26. 天青WebJobs包括PDB
- 27. 天青 - 認購配額
- 28. 天青Appsetting DBUSER不工作
- 29. 天青SuggestAsync地圖POCO
- 30. 天青功能webhook鍵
在此值得一提的是,類似文件夾的結構是通過斑點名稱中使用反斜線(\)來實現的。因此,我們可以使用CloudBlobDirectories,它只是一個具有相同前綴的斑點列表(即images \ someblob.jpg,images \ otherblob.jpg)。而單個blob的全名是「images \ someblob.jpg」) – astaykov
對@ astaykov的評論的修正:使用了正斜槓(/),這只是默認設置。該API可讓您指定所需的任何分隔符。 – smarx
目前在CloudBlobClient中沒有GetBlobDirectoryReference函數。要列出目錄的內容,你可以使用這個答案http://stackoverflow.com/a/25312000/1519458 –