我想将图像上传到我的 blob 存储容器中的特定子目录,但我不知道该怎么做。查看文档后,我可以看到,GetBlobs()
上有一个重载,它允许您指定前缀,但我看不到用于上传的前缀。这是我处理这个问题的方法。
上传位置为:uploads/car/17999/
CarController.cs
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Car car)
{
// Define the cancellation token.
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
if (ModelState.IsValid)
{
_carService.InsertCar(car);
int id = car.Id;
string pathPrefix = "car/17999";
string fileName = "car-image.jpg";
string strContainerName = "uploads";
BlobServiceClient blobServiceClient = new BlobServiceClient(accessKey);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(strContainerName);
//An example of how I would GET the blobs from a prefixed location, I don't know how to apply this to the upload part
//var blobs = containerClient.GetBlobs(0, 0, pathPrefix);
var blobs = containerClient.UploadBlob(fileName, car.ImageFile.OpenReadStream());
return RedirectToAction(nameof(Index));
}
return View(car);
}
最佳答案
请尝试更改您的fileName
并在其中添加pathPrefix
。像这样的东西:
string blobName = "car/17999/car-image.jpg";
containerClient.UploadBlob(blobName , car.ImageFile.OpenReadStream());
这应该将图像上传到 car/17999
虚拟文件夹中。
其他选择是使用 BlockBlobClient
并使用其 Upload
方法:
var connectionString = "UseDevelopmentStorage=true";
var containerName = "uploads";
var blobName = "car/17999/car-image.jpg";
BlockBlobClient blockBlobClient = new BlockBlobClient(connectionString, containerName, blobName);
blockBlobClient.Upload(car.ImageFile.OpenReadStream());
关于c# - 如何从 ASP.NET Core 应用程序将图像上传到 Azure Blob 存储中的特定目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61056490/