如何在ASP.NET Core应用程序内的容器中返回blob列表?

问题描述 投票:0回答:1

我正在尝试整理一种方法来获取指定容器中的Blob列表,并将其返回到ASP.NET Core应用程序中的视图。查看Microsoft文档,我看不到保存控制台应用程序后完成此操作的任何示例,但这使我对实现更加困惑。

这里是代码,从我的EditHomeController方法开始。这很简单,传入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();
}

到此为止,我被困住了,这种方法对列出我定义的目录的内容是正确的,如果是的话,我如何将其返回到要使用的视图中?

c# entity-framework asp.net-core azure-storage azure-storage-blobs
1个回答
0
投票

我认为您正在使用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();
        }
    }
}

输出

enter image description here

您也可以下载blob的内容,请检查此link

© www.soinside.com 2019 - 2024. All rights reserved.