![](/img/trans.png)
[英]How do I allow the overwriting of blobs from my ASP.NET Core application?
[英]How do I return a list of blobs in a container within my ASP.NET Core application?
我正在尝试干净地连接一个方法来获取指定容器中的 blob 列表并将其返回到我的 ASP.NET 核心应用程序中的视图。 查看 Microsoft 文档,我看不到任何这样做的示例保存控制台应用程序,但这让我对实现更加困惑。
这是代码,从我的HomeController
的Edit
方法开始。 这很简单,传入id
并返回数据,将 Id 传递给BlobStorageService
ListBlobsAsync
方法以识别所有文件的正确容器。
家庭控制器/编辑
[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 package。 如果你可以使用Azure.Storage.Blobs
package 然后试试下面的代码。
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();
}
}
}
Output
您也可以下载 blob 的内容,查看此链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.