在尝试访问Azure blob文件夹的所有文件时,获取container.ListBlobs();
的示例代码,但是看起来像是一个旧的代码。
旧代码:container.ListBlobs();
尝试新代码:container.ListBlobsSegmentedAsync(continuationToken);
我正在尝试使用以下代码:
container.ListBlobsSegmentedAsync(continuationToken);
文件夹就像:
Container/F1/file.json
Container/F1/F2/file.json
Container/F2/file.json
正在寻找更新版本以从Azure文件夹中获取所有文件。 任何示例代码都会有所帮助,谢谢!
答案 0 :(得分:3)
C#代码:
//connection string
string storageAccount_connectionString = "**NOTE: CONNECTION STRING**";
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("**NOTE:NAME OF CONTAINER**");
//The specified container does not exist
try
{
//root directory
CloudBlobDirectory dira = container.GetDirectoryReference(string.Empty);
//true for all sub directories else false
var rootDirFolders = dira.ListBlobsSegmentedAsync(true, BlobListingDetails.Metadata, null, null, null, null).Result;
foreach (var blob in rootDirFolders.Results)
{
Console.WriteLine("Blob", blob);
}
}
catch (Exception e)
{
// Block of code to handle errors
Console.WriteLine("Error", e);
}
答案 1 :(得分:2)
这是答案的代码:
private async Task<List<IListBlobItem>> ListBlobsAsync(CloudBlobContainer container)
{
BlobContinuationToken continuationToken = null;
List<IListBlobItem> results = new List<IListBlobItem>();
do
{
bool useFlatBlobListing = true;
BlobListingDetails blobListingDetails = BlobListingDetails.None;
int maxBlobsPerRequest = 500;
var response = await container.ListBlobsSegmentedAsync(BOAppSettings.ConfigServiceEnvironment, useFlatBlobListing, blobListingDetails, maxBlobsPerRequest, continuationToken, null, null);
continuationToken = response.ContinuationToken;
results.AddRange(response.Results);
}
while (continuationToken != null);
return results;
}
然后您可以返回如下值:
IEnumerable<IListBlobItem> listBlobs = await this.ListBlobsAsync(container);
foreach(CloudBlockBlob cloudBlockBlob in listBlobs)
{
BOBlobFilesViewModel boBlobFilesViewModel = new BOBlobFilesViewModel
{
CacheKey = cloudBlockBlob.Name,
Name = cloudBlockBlob.Name
};
listBOBlobFilesViewModel.Add(boBlobFilesViewModel);
}
//return listBOBlobFilesViewModel;
答案 2 :(得分:0)
方法CloudBlobClient.ListBlobsSegmentedAsync
用于返回包含容器中blob项目集合的结果段。
要列出所有斑点,我们可以使用ListBlobs
方法
以下是演示供您参考:
public static List<V> ListAllBlobs<T, V>(Expression<Func<T, V>> expression, string containerName,string prefix)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("YourConnectionString;");
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
var list = container.ListBlobs(prefix: prefix,useFlatBlobListing: true);
List<V> data = list.OfType<T>().Select(expression.Compile()).ToList();
return data;
}
用法和屏幕截图:
在一个文件夹下列出所有blob的名称:
在一个文件夹下列出所有Blob的URL:
答案 3 :(得分:0)
更新: 使用 Azure.Storage.Blobs v12 - Package
从目录中获取所有文件名var storageConnectionString = "DefaultEndpointsProtocol=...........=core.windows.net";
var blobServiceClient = new BlobServiceClient(storageConnectionString);
//get container
var container = _blobServiceClient.GetBlobContainerClient("container_name");
List<string> blobNames = new List<string>();
//Enumerating the blobs may make multiple requests to the service while fetching all the values
//Blobs are ordered lexicographically by name
//if you want metadata set BlobTraits - BlobTraits.Metadata
var blobs = container.GetBlobsByHierarchyAsync(BlobTraits.None, BlobStates.None, "/");
await foreach (var blob in blobs)
{
//check if the blob is a virtual directory.
if (blob.IsPrefix)
{
// You can also access files under nested folders in this way,
//of course you will need to create a function accordingly (you can do a recursive function)
//var prefix = blob.Name; // blob.Name = "folderA\"
//var blobs = container.GetBlobsByHierarchyAsync(BlobTraits.None, BlobStates.None, "/", prefix);
}
else
{
blobNames.Add(blob.Name);
}
}
您可以找到更多选项和示例here。
这是 nuget 包的 link。