如何从 ASP.NET 核心中的私人 Azure Blob 存储帐户 return 图像到视图?
How do I return an image to a view from a private Azure Blob Stroage account in ASP.NET Core?
我已将我的 Azure blob 存储容器设置为私有,并且我有一个已在我的 asp.net 核心应用程序中定义的共享访问密钥。对于诸如列出 blob 存储内容之类的工作,它运行良好,但现在我发现自己需要带回一个图像文件以在我的视图中显示。问题是,当我 return blob 时,是的,我可以在控制器中访问它,但它只是 returns 字符串到它的位置,访问该位置没有访问密钥 returns错误 Resource not found
,这是预期的。
我的问题是,如何 return 需要我的视图的共享访问密钥的图像 blob?到目前为止,这是我的代码,其中只有 returns Uri 作为字符串,由于上述原因没有任何用处。我需要下载图像并暂时存储它还是什么?
CarController 使用
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);
}
要从私有容器中读取,您需要在 blob 或至少具有 Read
权限的 blob 容器上创建 Shared Access Signature
。
这是您可以使用的代码。在此,我在具有 Read
权限的 blob 上创建一个 SAS 令牌,该权限将在创建后 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
};
我已将我的 Azure blob 存储容器设置为私有,并且我有一个已在我的 asp.net 核心应用程序中定义的共享访问密钥。对于诸如列出 blob 存储内容之类的工作,它运行良好,但现在我发现自己需要带回一个图像文件以在我的视图中显示。问题是,当我 return blob 时,是的,我可以在控制器中访问它,但它只是 returns 字符串到它的位置,访问该位置没有访问密钥 returns错误 Resource not found
,这是预期的。
我的问题是,如何 return 需要我的视图的共享访问密钥的图像 blob?到目前为止,这是我的代码,其中只有 returns Uri 作为字符串,由于上述原因没有任何用处。我需要下载图像并暂时存储它还是什么?
CarController 使用
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);
}
要从私有容器中读取,您需要在 blob 或至少具有 Read
权限的 blob 容器上创建 Shared Access Signature
。
这是您可以使用的代码。在此,我在具有 Read
权限的 blob 上创建一个 SAS 令牌,该权限将在创建后 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
};