azure 如何允许覆盖ASP.NETCore应用程序中的Blob?

r7knjye2  于 2023-03-31  发布在  .NET
关注(0)|答案(3)|浏览(162)

用户可以在创建记录时上传图像,当您编辑该记录并尝试上传新图像时,会出现“This blob already exists”错误。是否有一种方法可以在应用程序中启用覆盖同名blob?
下面是我处理更新过程的代码。
需要注意的是,为了应用程序的需要,我创建了一个图像的三次迭代,因此我包含了包含该信息的数组。

CarController.cs

private readonly int[] sizeArray = new int[] { 700, 350, 150 };

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Car car)
{
    if (ModelState.IsValid)
    {
        //Create the Car
                _carService.InsertCar(car);

                //Get the ID of the newly created car
                int id = car.Id;

                //Define the container name for Azure Storage
                string strContainerName = "uploads";                               

                //Blob Client
                BlobServiceClient blobServiceClient = new BlobServiceClient(accessKey);
                BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(strContainerName);

                //Iterate over the array of image sizes
                foreach (var imageSize in sizeArray)
                {
                    try
                    {
                        //Pass image to image processor and save to the blob location
                        string fileName = "car/" + id + "/car-image-" + imageSize + ".jpg";
                        Stream returnStream = ProcessImage(imageSize, car);
                        containerClient.UploadBlob(fileName, returnStream);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }                   
                return RedirectToAction(nameof(Index));
            }            
            return View(car);
        }
jhiyze9q

jhiyze9q1#

我想你用的是v12 client library
那么在容器级就没有blob覆盖方法了,应该使用BlobClientUpload(Stream content, bool overwrite = false)方法,示例代码如下:

BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(mycontainer);
            BlobClient blobClient = containerClient.GetBlobClient("blob_name");

            blobClient.Upload(your_stream, overwrite: true);
zbdgwd5y

zbdgwd5y2#

如果你想同时指定overwrite: true和使用BlobStorageOptions,这是Upload(...)重载的实现,带有bool overwrite参数:

public virtual Response<BlobContentInfo> Upload(
    Stream content,
    bool overwrite = false,
    CancellationToken cancellationToken = default) =>
    Upload(
        content,
        conditions: overwrite ? null : new BlobRequestConditions { IfNoneMatch = new ETag(Constants.Wildcard) },
        cancellationToken: cancellationToken);

这将导致以下重载(为简洁起见,省略了一些代码):

public virtual Response<BlobContentInfo> Upload(
    ....
    BlobRequestConditions conditions = default,
    ....) =>
    StagedUploadInternal(
        content,
        new BlobUploadOptions
        {
            ...
            Conditions = conditions,
            ...
        },
        ...

因此,实际上,只要您指定自己的BlobUploadOptions,而没有任何特定的IfNoneMatch选项,overwrite就是默认值。
这也得到了函数注解的支持:

// Summary:
//     The Azure.Storage.Blobs.BlobClient.UploadAsync(System.IO.Stream,Azure.Storage.Blobs.Models.BlobUploadOptions,System.Threading.CancellationToken)
//     operation overwrites the contents of the blob, creating a new block blob if none
//     exists. Overwriting an existing block blob replaces any existing metadata on
//     the blob. Set access conditions through Azure.Storage.Blobs.Models.BlobUploadOptions.Conditions
//     to avoid overwriting existing data.

BlobClient.cs复制的代码。

yhqotfr8

yhqotfr83#

正如documentation所述:
有关部分块blob更新和其他高级功能,请参阅BlockBlobClient。要创建或修改页面或追加blob,请参阅PageBlobClient或AppendBlobClient。
因此,您可能应该使用BlockBlobClient或可更新的东西。

相关问题