我正在尝试整理一种方法来获取指定容器中的Blob列表,并将其返回到ASP.NET Core应用程序中的视图。查看Microsoft文档,我看不到保存控制台应用程序后完成此操作的任何示例,但这使我对实现更加困惑。
这里是代码,从我的Edit
的HomeController
方法开始。这很简单,传入id
并返回数据,然后将ID传递给BlobStorageService
ListBlobsAsync
方法以为所有文件标识正确的容器。
HomeController / Edit
[HttpGet]
public IActionResult Edit(int id)
{
var car = _carService.GetCar(id);
BlobStorageService objBlob = new BlobStorageService(accessKey);
objBlob.ListBlobsAsync(car.Id.ToString());
return View(car);
}
这里是我的BlobStorageService.cs
BlobStorageService
public async void ListBlobsAsync(string id)
{
//Folder Structure /uploads/car/<id>/contents
string strContainerName = "uploads";
string pathPrefix = "car";
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(accessKey);
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(strContainerName);
CloudBlobDirectory blobDirectory = cloudBlobContainer.GetDirectoryReference(pathPrefix);
CloudBlockBlob blockBlob = blobDirectory.GetBlockBlobReference(id);
await blockBlob.DownloadBlockListAsync();
}
到此为止,我被困住了,这种方法对列出我定义的目录的内容是正确的,如果是的话,我如何将其返回到要使用的视图中?
我认为您正在使用Microsoft.WindowsAzure.Storage程序包。如果可以使用Azure.Storage.Blobs
软件包,请尝试以下代码。
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System;
namespace ConsoleApp2
{
class Program
{
static string connectionString = "DefaultEndpointsProtocol=https;AccountName=storage******c9709;AccountKey=v**************************************;EndpointSuffix=core.windows.net";
static string container = "azure-webjobs-hosts";
static void Main(string[] args)
{
// Get a reference to a container named "sample-container" and then create it
BlobContainerClient blobContainerClient = new BlobContainerClient(connectionString, container);
blobContainerClient.CreateIfNotExists();
Console.WriteLine("Listing blobs...");
// List all blobs in the container
var blobs = blobContainerClient.GetBlobs();
foreach (BlobItem blobItem in blobs)
{
Console.WriteLine("\t" + blobItem.Name);
}
Console.Read();
}
}
}
输出
您也可以下载blob的内容,请检查此link