如何允许覆盖 ASP.NET Core 应用程序中的 blob?

Yan*_*aya 14 c# azure azure-blob-storage asp.net-core

用户可以在创建记录时上传图像,当您编辑该记录并尝试上传新图像时,会出现“此 blob 已存在”错误。有没有办法可以在我的应用程序中启用同名 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);
        }
Run Code Online (Sandbox Code Playgroud)

Iva*_*ang 28

我认为您正在使用v12 客户端库

那么容器级别没有blob覆盖方法,你应该Upload(Stream content, bool overwrite = false)使用BlobClient. 示例代码如下:

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

            blobClient.Upload(your_stream, overwrite: true);
Run Code Online (Sandbox Code Playgroud)

  • Upload 似乎没有接受布尔覆盖参数和 BlobUploadOptions (用于设置元数据)的重载。我错过了什么吗? (11认同)
  • UploadAsync 函数重载采用 BlobUploadOptions 来覆盖 blob。如果您不想覆盖,则必须在选项中设置条件。https://learn.microsoft.com/en-us/dotnet/api/azure.storage.blobs.blobclient.upload?view=azure-dotnet#azure-storage-blobs-blobclient-upload(system-io-stream- azure-存储-blob-模型-blobuploadoptions-系统-线程-取消令牌) (3认同)

Wil*_*lem 21

如果你想同时指定两者overwrite: true并使用,这是带有参数的重载的实现:BlobStorageOptionsUpload(...)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);
Run Code Online (Sandbox Code Playgroud)

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

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

overwrite因此,只要您在BlobUploadOptions没有任何特定选项的情况下指定自己的值,实际上就是默认值IfNoneMatch

该函数的注释也支持了这一点:

// 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.
Run Code Online (Sandbox Code Playgroud)

代码复制自BlobClient.cs.