我正在开发社交网络应用程序。
我想在azure存储中为每个加入社交网络的用户(客户端)创建一个容器,这意味着user1有一个名为容器1的容器,而容器1内部将有xml格式的用户1个配置文件和用户1的个人资料图片。
同样,对于user2,将在azure blob存储中创建容器2,然后用户2配置文件以xml格式和用户2的个人资料图片保存,它就像这样,因此,对于10个用户来说,将会有10个容器。
如果我想在用户客户端1的不同9个容器中列出存储在Azure存储中的所有9个用户的信息,我该怎么办?
我正在使用webservice,但我面临的挑战是如何收集位于9个不同容器中的所有9个用户个人资料信息。
答案 0 :(得分:44)
以下应该做的 -
CloudStorageAccount account =
CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
// We need to access blobs now, so create a CloudBlobClient
CloudBlobClient blobClient = account.CreateCloudBlobClient();
IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers();
答案 1 :(得分:4)
对于较新版本的Microsoft.WindowsAzure.Storage ,请使用CloudBlobClient.ListContainersSegmentedAsync
方法的正确变体。用法示例如下:
private static async Task<IEnumerable<CloudBlobContainer>> ListContainersAsync(CloudBlobClient cloudBlobClient)
{
BlobContinuationToken continuationToken = null;
var containers = new List<CloudBlobContainer>();
do
{
ContainerResultSegment response = await cloudBlobClient.ListContainersSegmentedAsync(continuationToken);
continuationToken = response.ContinuationToken;
containers.AddRange(response.Results);
} while (continuationToken != null);
return containers;
}
上述方法的用法如下:
string connectionString = "<your connection string>";
CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount storageAccount);
if (storageAccount == null)
{
Console.WriteLine("Connection string did not work");
}
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
var containers = await ListContainersAsync(cloudBlobClient);
或者,您可以创建如下扩展方法:
public static class CloudBlobClientExtension
{
public static async Task<IEnumerable<CloudBlobContainer>> ListContainersAsync(this CloudBlobClient cloudBlobClient)
{
BlobContinuationToken continuationToken = null;
var containers = new List<CloudBlobContainer>();
do
{
ContainerResultSegment response = await cloudBlobClient.ListContainersSegmentedAsync(continuationToken);
continuationToken = response.ContinuationToken;
containers.AddRange(response.Results);
} while (continuationToken != null);
return containers;
}
}
上述扩展方法的用法如下:
string connectionString = "<your connection string>";
CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount storageAccount);
if (storageAccount == null)
{
Console.WriteLine("Connection string did not work");
}
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
IEnumerable<CloudBlobContainer> containers = await cloudBlobClient.ListContainersAsync();
有关更多详细信息,请参阅MSDN和Balkan's Blog
对于较旧的Azure存储版本,请使用CloudBlobClient.ListContainers
方法。用法示例如下:
string connectionString = "<your connection string>";
CloudStorageAccount.TryParse(connectionString, out CloudStorageAccount storageAccount);
if (storageAccount == null)
{
Console.WriteLine("Connection string did not work");
}
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
var containers = cloudBlobClient.ListContainers();
答案 2 :(得分:0)
CloudStorageAccount account =
CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
// We need to access blobs now, so create a CloudBlobClient
blobClient = account.CreateCloudBlobClient();
IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers();
// This will return you list of containers
var containerList = containers.Select(e => e.Name).Tolist();
答案 3 :(得分:0)
using Azure;
using Azure.Storage.Blobs;
BlobServiceClient blobServiceClient = new BlobServiceClient(azStorageConnString);
var containerList = blobServiceClient.GetBlobContainers();
foreach (var container in containerList)
// doImportantWork