我正在尝试为容器中的所有blob创建一个函数。我接受了帮助
How to get hold of all the blobs in a Blob container which has sub directories levels(n levels)?,它似乎使用了不再存在的重载。我在其他字段prefix
和operationContext
中添加了默认值:
static internal async Task<List<string>> ListBlobNames(string ContainerName)
{
BlobContinuationToken continuationToken = null;
bool useFlatBlobListing = true;
BlobListingDetails blobListingDetails = BlobListingDetails.None;
var blobOptions = new BlobRequestOptions();
CloudBlobContainer container = Container(ContainerName);
var operationContext = new OperationContext();
var verify = container.GetBlobReference("A_Valid_Name.jpg");
var verify2 = container.GetBlobReference("NotAName.jpg");
using (var a = await verify.OpenReadAsync()) ;
//using (var a = await verify2.OpenReadAsync()); // doesn't work since it doesn't exist
return (await container.ListBlobsSegmentedAsync("", useFlatBlobListing, blobListingDetails, null, continuationToken, blobOptions, operationContext))
.Results.Select(s => s.Uri.LocalPath.ToString()).ToList();
}
最后一行给了我一个例外:
StorageException:请求的URI不代表服务器上的任何资源。
然后我创建了verfiy
和verify2
变量来测试我的容器是否有效。 verify
引用有效的blob,verify2
引用无效的blob名称。使用第二个using语句运行代码取消注释在第二个using语句中给出了一个错误。这表明verify
有效,因此容器有效。
答案 0 :(得分:4)
我正在尝试为容器中的所有blob创建一个函数。
您可以利用Azure存储客户端库并安装包WindowsAzure.Storage,然后您可以按照教程List blobs in pages asynchronously来实现您的目的。为了测试,我刚刚创建了我的.Net Core控制台应用程序,如下所示:
static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}
static async Task MainAsync(string[] args)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("{your-storage-connection-string}");
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
string containerName = "images";
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
if (await container.ExistsAsync())
await ListBlobsSegmentedInFlatListing(container);
else
Console.WriteLine($"Your container with the name:{containerName} does not exist!!!");
Console.WriteLine("press any key to exit...");
Console.ReadLine();
}
<强> ListBlobsSegmentedInFlatListing:强>
async public static Task ListBlobsSegmentedInFlatListing(CloudBlobContainer container)
{
//List blobs to the console window, with paging.
Console.WriteLine("List blobs in pages:");
int i = 0;
BlobContinuationToken continuationToken = null;
BlobResultSegment resultSegment = null;
//Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
//When the continuation token is null, the last page has been returned and execution can exit the loop.
do
{
//This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
//or by calling a different overload.
resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);
if (resultSegment.Results.Count<IListBlobItem>() > 0) { Console.WriteLine("Page {0}:", ++i); }
foreach (var blobItem in resultSegment.Results)
{
Console.WriteLine("\t{0}", blobItem.StorageUri.PrimaryUri);
}
Console.WriteLine();
//Get the continuation token.
continuationToken = resultSegment.ContinuationToken;
}
while (continuationToken != null);
}
<强>测试强>
答案 1 :(得分:0)
这对我有用......
String myContainerName = "images";
CloudBlobContainer blobContainer = blobClient.GetContainerReference(myContainerName);
CloudBlockBlob blockBlob;
blockBlob = blobContainer.GetBlockBlobReference("NotAName.jpg");
bool verify2 = await blockBlob.ExistsAsync();
if (!verify2)
{
// the blob image does not exist
// do something
}