【发布时间】:2020-07-18 11:29:19
【问题描述】:
我已将我的 Azure blob 存储容器设置为私有,并且我有一个在我的 asp.net 核心应用程序中定义的共享访问密钥。对于列出 blob 存储内容等工作,它运行良好,但现在我发现自己需要带回一个图像文件以显示在我的视图中。问题是当我返回 blob 时,是的,我可以在控制器中访问它,但它只是将字符串返回到它的位置,在没有访问密钥的情况下访问该位置会返回错误 Resource not found,这是预期的。
我的问题是,如何将需要共享访问密钥的图像 blob 返回到我的视图?到目前为止,这是我的代码,它仅将 Uri 作为字符串返回,由于上述原因,它没有任何用处。我需要下载图像并临时存储它吗?
汽车控制器使用
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
CarController.cs
[HttpGet]
public IActionResult Edit(int id)
{
var car = _carService.GetCar(id);
string strContainerName = "uploads";
string filePrefix = "car/" + id + "/car-image.jpg";
var filelist = new List<BlobListViewModel>();
BlobServiceClient blobServiceClient = new BlobServiceClient(accessKey);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(strContainerName);
//Get the specific image
var blob = containerClient.GetBlobClient(filePrefix);
//I'm binding to a view model as a string, which, as I say, doesn't work for security reasons.
var data = new BlobListViewModel {
FileName = blob.Uri.ToString()
};
return View(data);
}
【问题讨论】:
标签: c# asp.net-core blob azure-blob-storage