如何从ASP.NET Core应用程序将图像上传到Azure blob存储中的特定目录?

问题描述 投票:0回答:1

我想将图像上传到我的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);
    }
c# azure asp.net-core azure-storage-blobs
1个回答
0
投票

[请尝试通过更改fileName并在其前添加pathPrefix。类似于:

string blobName = "car/17999/car-image.jpg";
containerClient.UploadBlob(blobName , car.ImageFile.OpenReadStream());

这应该将图像上传到car/17999虚拟文件夹中。

其他替代方法是使用BlockBlobClient并使用其BlockBlobClient方法:

Upload
© www.soinside.com 2019 - 2024. All rights reserved.