C# 如何从ASP.NET Core中的私有Azure Blob Stroage帐户将图像返回到视图?
我已将Azure blob存储容器设置为private,并在asp.net核心应用程序中定义了一个共享访问密钥。对于诸如列出blob存储的内容之类的工作,它工作得很好,但现在我发现自己需要带回一个图像文件,以便在视图中显示。问题是,当我返回blob时,是的,我可以在控制器中访问它,但它只是将字符串返回到它的位置,在不使用access键的情况下访问该位置会返回错误C# 如何从ASP.NET Core中的私有Azure Blob Stroage帐户将图像返回到视图?,c#,asp.net-core,blob,azure-storage-blobs,C#,Asp.net Core,Blob,Azure Storage Blobs,我已将Azure blob存储容器设置为private,并在asp.net核心应用程序中定义了一个共享访问密钥。对于诸如列出blob存储的内容之类的工作,它工作得很好,但现在我发现自己需要带回一个图像文件,以便在视图中显示。问题是,当我返回blob时,是的,我可以在控制器中访问它,但它只是将字符串返回到它的位置,在不使用access键的情况下访问该位置会返回错误Resource not found,这是预期的 我的问题是,如何返回需要共享访问密钥的图像blob到我的视图?这里是我的代码,到目前为
Resource not found
,这是预期的
我的问题是,如何返回需要共享访问密钥的图像blob到我的视图?这里是我的代码,到目前为止,它只返回一个字符串形式的Uri,由于上述原因,它没有任何用处。我需要下载图像并暂时保存吗
汽车控制器的使用
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
[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);
}
CarController.cs
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
[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);
}
[HttpGet]
公共IActionResult编辑(内部id)
{
var car=\u carService.GetCar(id);
字符串strContainerName=“上传”;
字符串filePrefix=“car/”+id+“/car image.jpg”;
var filelist=新列表();
BlobServiceClient BlobServiceClient=新BlobServiceClient(accessKey);
BlobContainerClient containerClient=blobServiceClient.GetBlobContainerClient(strContainerName);
//获取特定的图像
var blob=containerClient.GetBlobClient(filePrefix);
//我将以字符串的形式绑定到视图模型,正如我所说的,出于安全原因,它不起作用。
var数据=新的BlobListViewModel{
FileName=blob.Uri.ToString()
};
返回视图(数据);
}
要从专用容器读取,您需要在blob或blob容器上创建至少具有读取
权限的
这是您可以使用的代码。在本例中,我将在blob上创建一个SAS令牌,该令牌具有Read
权限,该权限将在创建时1分钟后过期
BlobSasBuilder blobSasBuilder = new BlobSasBuilder()
{
BlobContainerName = strContainerName,
BlobName = filePrefix,
ExpiresOn = DateTime.UtcNow.AddMinutes(1),
};
blobSasBuilder.SetPermissions(BlobAccountSasPermissions.Read);
var sasToken = blobSasBuilder.ToSasQueryParameters(new Azure.Storage.StorageSharedKeyCredential(accountName, accountKey)).ToString();
var blob = containerClient.GetBlobClient(filePrefix);
var blobSasUrl = blob.Uri.ToString() + "?" + sasToken;
//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 = blobSasUrl
};