任何人都可以告诉我,为什么我们不能在天蓝色存储中的容器内创建容器?还有哪些方法需要在azure存储中创建目录层次结构?
答案 0 :(得分:14)
您无法在容器中创建容器,因为Windows Azure根本不支持层次容器(您应该将容器视为“磁盘驱动器”,就像 C:\ 磁盘一样)。但是通过 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 :(得分:1)
以下是工作项目中我的类(用于管理blob)的azure容器的工作代码 请注意,容器中文件夹的命名和容器中的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();
}
}
}
答案 2 :(得分:1)
在Azure blob存储的另一个容器中创建容器的最简单方法是使用Cerebrata中名为Azure Explorer的免费Azure存储管理工具。
它允许您通过创建新文件夹来创建容器。