【发布时间】:2011-03-12 03:54:39
【问题描述】:
如何在 azure 存储位置创建子容器?
【问题讨论】:
标签: azure azure-storage azure-blob-storage
如何在 azure 存储位置创建子容器?
【问题讨论】:
标签: azure azure-storage azure-blob-storage
您指的是 blob 存储吗?如果是这样,那么层次结构就是 StorageAccount/Container/BlobName。没有嵌套容器。
话虽如此,您可以在 blob 名称中使用斜杠来模拟 URI 中的嵌套容器。命名细节见this article on MSDN。
【讨论】:
Windows Azure 不提供层次容器的概念,但它提供了一种机制来通过约定和 API 遍历层次结构。所有容器都存放在同一层。您可以通过对 blob 名称使用命名约定来获得类似的功能。
例如,您可以创建一个名为“content”的容器,并在该容器中创建具有以下名称的 blob:
content/blue/images/logo.jpg
content/blue/images/icon-start.jpg
content/blue/images/icon-stop.jpg
content/red/images/logo.jpg
content/red/images/icon-start.jpg
content/red/images/icon-stop.jpg
注意,这些 blob 是针对您的“内容”容器的平面列表。也就是说,使用“/”作为常规分隔符,为您提供了以分层方式遍历它们的功能。
protected IEnumerable<IListBlobItem>
GetDirectoryList(string directoryName, string subDirectoryName)
{
CloudStorageAccount account =
CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
CloudBlobClient client =
account.CreateCloudBlobClient();
CloudBlobDirectory directory =
client.GetBlobDirectoryReference(directoryName);
CloudBlobDirectory subDirectory =
directory.GetSubdirectory(subDirectoryName);
return subDirectory.ListBlobs();
}
然后你可以这样称呼它:
GetDirectoryList("content/blue", "images")
注意使用 GetBlobDirectoryReference 和 GetSubDirectory 方法和 CloudBlobDirectory 类型而不是 CloudBlobContainer。这些提供了您可能正在寻找的遍历功能。
这应该可以帮助您入门。如果这不能回答您的问题,请告诉我:
[感谢Neil Mackenzie的启发]
【讨论】:
GetBlobDirectoryReference 位不起作用。相反,我们可以使用以下内容:CloudBlobContainer container = cloudBlobClient.GetContainerReference(directoryName);CloudBlobDirectory subDirectory = container.GetDirectoryReference(subDirectoryName); 等...
cloudBlobClient 还是client?
我同意 tobint 的回答,我想在这种情况下添加一些内容,因为我也 我需要以相同的方式将我的游戏 html 上传到 Azure 存储并创建此目录:
所以在您推荐后,我尝试使用 Azure 存储资源管理器工具上传我的内容,您可以使用此 url 下载工具和源代码:Azure Storage Explorer
首先我尝试通过工具上传,但它不允许分层目录上传,因为你不需要:How to create sub directory in a blob container
最后,我调试了 Azure 存储资源管理器源代码,并在 StorageAccountViewModel.cs 文件中编辑了 Background_UploadBlobs 方法和 UploadFileList 字段。您可以根据需要编辑它。我可能犯了拼写错误:/ 很抱歉,但这只是我的建议。
【讨论】:
示例代码
string myfolder = "<folderName>";
string myfilename = "<fileName>";
string fileName = String.Format("{0}/{1}.csv", myfolder, myfilename);
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
【讨论】:
如果您想从 Azure 门户上传文件: 要在容器中创建子文件夹,在上传文件时,您可以转到高级选项并选择上传到文件夹,这将在容器中创建一个新文件夹并将文件上传到该文件夹中。
【讨论】: