我正在尝试打印容器中 blob 的名称



我试图在容器中打印blobs的名称,但是在

行之后没有任何打印
List<BlobItem> segment = await blobContainer.GetBlobsAsync().ToListAsync();

完整代码:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System.Data.Entity;
using Azure.Storage.Sas;
using System.Linq;
namespace IterateBlobs
{
/*Main code for iterating over blobs*/
class Program
{
static void Main(string[] args)
{
Task iterateBlobs = IterateBlobsAsync();
}
private static async Task IterateBlobsAsync()
{
String connect = "connection string";
BlobServiceClient sourceClient = new BlobServiceClient(connect);
BlobContainerClient blobContainer = sourceClient.GetBlobContainerClient("container name");
// Iterate through the blobs in a container
List<BlobItem> segment = await blobContainer.GetBlobsAsync().ToListAsync();
foreach (BlobItem blobItem in segment)
{
Console.WriteLine(blobItem.Name + " ");
BlobClient blob = blobContainer.GetBlobClient(blobItem.Name);
// Check the source file's metadata
Response<BlobProperties> propertiesResponse = await blob.GetPropertiesAsync();
BlobProperties properties = propertiesResponse.Value;
}
}
}
}

很高兴你已经解决了这个问题。

感谢@n0rd将您的建议作为答案,以便对其他社区成员有益。

如果你正在迭代Blob容器和文件你必须使用异步任务.

在你的代码中,你正在迭代blob容器,但在主块中,它不会等待所有迭代完成. 一旦从获得任何响应,它将返回IterateBlobsAsync. 它将从主退出。方法,这就是您没有获得任何容器信息的原因。

要避免这种情况,您必须遵循下面的

修改代码
class Program
{
static async Task Main(string[] args)
{
Task iterateBlobs = await IterateBlobsAsync();
}
private static async Task IterateBlobsAsync()
{
String connect = "connection string";
BlobServiceClient sourceClient = new BlobServiceClient(connect);
BlobContainerClient blobContainer = sourceClient.GetBlobContainerClient("container name");
// Iterate through the blobs in a container
List<BlobItem> segment = await blobContainer.GetBlobsAsync();
foreach (BlobItem blobItem in segment)
{
Console.WriteLine(blobItem.Name + " ");
BlobClient blob = blobContainer.GetBlobClient(blobItem.Name);
// Check the source file's metadata
Response<BlobProperties> propertiesResponse = await blob.GetPropertiesAsync();
BlobProperties properties = propertiesResponse.Value;
}
}
}
}

参考MSDOC列出blob容器。

最新更新