我想将图像上传到我的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
并使用其BlockBlobClient
方法:
Upload