I would like to upload an image to a specific subdirectory within my blob storage container and I'm not sure how to do it. Having looked at the documentation I can see that, there is an overload on the GetBlobs() which allows you to specify a prefix but I can't see one for uploading. Here is my method that handles this.
Upload location would be: 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);
}