【问题标题】:BlockBlobClient.UploadAsync fails when trying to modify the incoming WatchableReadStream object尝试修改传入的 WatchableReadStream 对象时,BlockBlobClient.UploadAsync 失败
【发布时间】:2021-05-23 06:28:18
【问题描述】:

我希望你有一些建议,因为我几乎没有选择。我正在尝试做的事情:

  • 通过 azure 门户将图像上传到 blob 容器
  • 执行此操作时,下方的 blobtrigger 会触发。
  • 我想调整传入图像的大小,它是一个Microsoft.Azure.WebJobs.Host.Blobs.WatchableReadStream 对象。
  • 我想将结果上传到新的 Blob 容器。

问题是,当我传递任何其他 Stream 时,例如MemoryStream 与未修改的 myBlob 对象相比,UploadAsync 方法似乎静默失败,BlobTrigger 再次触发,开始无限循环。写入当前的myBlob 对象也会静默失败。唯一有效的方法是在BlockBlobClient.UploadAsync 中传递myBlob 对象,而不进行任何修改,这是没用的。我正在 Azure 中进行远程调试会话。

此外,当我尝试将 MemoryStream 对象提供给 UploadAsync 方法时,也会出现同样的问题。下面的方法是blob触发器:

        public async Task UploadImage([BlobTrigger("mediafullsizecontainer/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
        {
            BlockBlobClient fullsizeBlobClient = _mediaFullsizeContainerClient.GetBlockBlobClient(name);
            Response<BlobProperties> blobPropertiesResponse = await fullsizeBlobClient.GetPropertiesAsync(null, default);

            BlobProperties blobProperties = blobPropertiesResponse.Value;

            //Only process blobs when the correct metadata properties are set
            if (blobProperties.Metadata.Any(property => property.Key == "category" & property.Value != String.Empty))
            {
                string category = blobProperties.Metadata["category"];

                Stream s = await ResizeImage(myBlob);

                BlockBlobClient thumbnailBlobclient = _mediaThumbnailContainerClient.GetBlockBlobClient(name);
                Response<BlobContentInfo> uploadResponse = await thumbnailBlobclient.UploadAsync(s, new BlobUploadOptions(), default);
                
                BlobContentInfo blobContentInfo = uploadResponse.Value;
            }
        }

ResizeImage 方法使用 SixLabors.ImageSharp 图像处理库。

        public async Task<Stream> ResizeImage(Stream inputStream)
        {
            (Image image, IImageFormat imageFormat) imageWithFormat = await Image.LoadWithFormatAsync(inputStream);
            int height = imageWithFormat.image.Height;
            int width = imageWithFormat.image.Width;

            imageWithFormat.image.Mutate(operation => {
                operation.Resize(width / 4, height / 4);
            });

            MemoryStream outputStream = new MemoryStream();
            imageWithFormat.image.Save(outputStream, imageWithFormat.imageFormat);

            return outputStream;
        }

所以当我像这样对 myBlob 对象进行模拟更改时,myBlob.Write(buffer) 失败:

                byte[] buffer = new byte[myBlob.Length];
                myBlob.Read(buffer, 0, (int)myBlob.Length);

                myBlob.Position = 0;
                myBlob.Write(buffer);

当我将myBlob 内容复制到MemoryStream 并将memoryStream 传递给UploadAsync 时,UploadAsync 失败:

                MemoryStream ms = new MemoryStream();
                myBlob.CopyTo(ms);
                 
                BlockBlobClient thumbnailBlobclient = _mediaThumbnailContainerClient.GetBlockBlobClient(name);
                Response<BlobContentInfo> uploadResponse = await thumbnailBlobclient.UploadAsync(ms, new BlobUploadOptions(), default);

唯一可行的方法是将 myBlob 对象传递给 UploadAsync 而不做任何修改,因为我需要修改传入的流,所以这是没用的:

        public async Task UploadImage([BlobTrigger("mediafullsizecontainer/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
        {
            BlockBlobClient fullsizeBlobClient = _mediaFullsizeContainerClient.GetBlockBlobClient(name);
            Response<BlobProperties> blobPropertiesResponse = await fullsizeBlobClient.GetPropertiesAsync(null, default);

            BlobProperties blobProperties = blobPropertiesResponse.Value;

            //Only process blobs when the correct metadata properties are set
            if (blobProperties.Metadata.Any(property => property.Key == "category" & property.Value != String.Empty))
            {
                string category = blobProperties.Metadata["category"];

                BlockBlobClient thumbnailBlobclient = _mediaThumbnailContainerClient.GetBlockBlobClient(name);
                Response<BlobContentInfo> uploadResponse = await thumbnailBlobclient.UploadAsync(myBlob, new BlobUploadOptions(), default);
                
                BlobContentInfo blobContentInfo = uploadResponse.Value;
            }
        }

如何将自定义流传递给 UploadAsync 方法? https://docs.microsoft.com/en-us/dotnet/api/azure.storage.blobs.specialized.blockblobclient.uploadasync?view=azure-dotnet 上的文档说您只需要一个流对象 - 但这不起作用。提前致谢。

【问题讨论】:

    标签: azure upload blob


    【解决方案1】:

    好吧,看起来我自己找到了答案,我摆脱了可怕的UploadAsync 方法,并通过将调整大小的图像的outputStream 读入缓冲区然后打开Stream 以写入thumbnailBlobclient 对象并将缓冲区传递给流对象。尝试修改触发器附带的传入myBlob 对象,再次触发触发器并创建循环是有道理的。

                    Stream outputStream = await ResizeAndCropImage(myBlob);
                    byte[] buffer = new byte[outputStream.Length];
                    outputStream.Read(buffer);
    
                    BlockBlobClient thumbnailBlobclient = _mediaThumbnailContainerClient.GetBlockBlobClient(name);
                    Stream s = thumbnailBlobclient.OpenWrite(true, new BlockBlobOpenWriteOptions { HttpHeaders = new BlobHttpHeaders { ContentType = "image/jpg" } }, default); //set http headers here as well?
    
                    await s.WriteAsync(buffer);
                    s.Close();
    

    我将以更通用的方式设置内容类型,但这个概念很有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-12
      • 2012-12-09
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 2011-02-16
      • 2019-05-17
      • 2011-10-24
      相关资源
      最近更新 更多