我需要列出Azure Blob文件名的名称。目前我能够列出所有带URL的文件,但我只需要列出名称。我想避免解析名称。能否请您查看我的以下代码和指南:
CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(blobConectionString);
var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
var backupContainer = backupBlobClient.GetContainerReference(container);
var list = backupContainer.ListBlobs();
答案 0 :(得分:29)
如果您正在使用 Windows Azure Storage 4.3.0 ,请尝试使用此代码。
List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
答案 1 :(得分:15)
这是完成此任务的另一种方法:
CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(blobConectionString);
var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
var backupContainer = backupBlobClient.GetContainerReference(container);
// useFlatBlobListing is true to ensure loading all files in
// virtual blob sub-folders as a plain list
var list = backupContainer.ListBlobs(useFlatBlobListing: true);
var listOfFileNames = new List<string>();
foreach (var blob in blobs) {
var blobFileName = blob.Uri.Segments.Last();
listOfFileNames.Add(blobFileName);
}
return listOfFileNames;
答案 2 :(得分:9)
我们可以获得一些其他信息,例如尺寸,修改日期和名称。
CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(YOUR_CON_STRING);
var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
var backupContainer = backupBlobClient.GetContainerReference("CONTAINER");
var blobs = backupContainer.ListBlobs().OfType<CloudBlockBlob>().ToList();
foreach (var blob in blobs)
{
string bName = blob.Name;
long bSize = blob.Properties.Length;
string bModifiedOn = blob.Properties.LastModified.ToString();
}
您也可以按名称下载特定文件。
// Download file by Name
string fileName = "Your_file_name";
CloudBlockBlob blobFile = backupContainer.GetBlockBlobReference(fileName);
blobFile.DownloadToFile(@"d:\"+ fileName, System.IO.FileMode.Create);
答案 3 :(得分:7)
这可用于 WindowsAzure.Storage 9.3.3 。
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
var cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
var blobResultSegment = await cloudBlobContainer.ListBlobsSegmentedAsync(continuationToken);
var blobs = blobResultSegment.Results.Select(i => i.Uri.Segments.Last()).ToList();
答案 4 :(得分:6)
详细解答。
// Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureBlobConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("container_name");
// Retrieve reference to a blob named "test.csv"
CloudBlockBlob blockBlob = container.GetBlockBlobReference("BlobName.tex");
//Gets List of Blobs
var list = container.ListBlobs();
List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
答案 5 :(得分:2)
您可以访问BlobProperties
获取名称:
foreach (object o in list)
{
BlobProperties bp = o as BlobProperties;
if (bp != null)
{
BlobProperties p = _Container.GetBlobProperties(bp.Name);
var name = p.Name; // get the name
}
}
答案 6 :(得分:1)
使用 Azure.Storage.Blobs v12 - 包获取 Azure blob 文件的名称列表
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.GetBlobsAsync(BlobTraits.None, BlobStates.None);
await foreach (var blob in blobs)
{
blobNames.Add(blob.Name);
}
您可以找到更多选项和示例here。
这是 nuget 包的 link。
答案 7 :(得分:0)
wal-g backup-fetch /home/postgres/pgdata/pgroot/data LATEST
INFO: 2019/12/19 10:06:24.765611 LATEST backup is: 'base_000000010000000000000003'
ERROR: 2019/12/19 10:06:25.062066 Failed to fetch backup: failed to fetch sentinel: context canceled
从文件列表中可以从天蓝色文件中获取所有文件
答案 8 :(得分:0)
ListBlobs
方法似乎不再存在。这是一个异步版本。
public static async Task<List<string>> ListBlobNamesAsync(CloudBlobContainer container)
{
var blobs = await ListBlobsAsync(container);
return blobs.Cast<CloudBlockBlob>().Select(b => b.Name).ToList();
//Alternate version
//return blobs.Select(b => b.Uri.ToString()).Select(s => s.Substring(s.LastIndexOf('/') + 1)).ToList();
}
public static async Task<List<IListBlobItem>> ListBlobsAsync(CloudBlobContainer container)
{
BlobContinuationToken continuationToken = null; //start at the beginning
var results = new List<IListBlobItem>();
do
{
var response = await container.ListBlobsSegmentedAsync(continuationToken);
continuationToken = response.ContinuationToken;
results.AddRange(response.Results);
}
while (continuationToken != null); //when this is null again, we've reached the end
return results;
}
答案 9 :(得分:0)
我们必须使用ListBlobsSegmentedAsync()
方法,然后我们可以通过以下代码找出blob:
public CloudBlockBlob GetLatestBlobByBlobNamePattern(CloudBlobContainer container, string blobNamePattern)
{
var root = container.GetDirectoryReference(string.Empty);
var blobsList = root.ListBlobsSegmentedAsync(null);
blobsList.Wait();
BlobResultSegment segment = blobsList.Result;
List<IListBlobItem> list = new List<IListBlobItem>();
list.AddRange(segment.Results);
while (segment.ContinuationToken != null)
{
var blobs = container.ListBlobsSegmentedAsync(segment.ContinuationToken);
blobs.Wait();
segment = blobs.Result;
list.AddRange(segment.Results);
}
var blob = list.Where(x => x.Uri.Segments.Last().Contains(blobNamePattern)).FirstOrDefault();
return (CloudBlockBlob)blob;
}