【发布时间】: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 上的文档说您只需要一个流对象 - 但这不起作用。提前致谢。
【问题讨论】: