Azure Functions 使用两个输出 blob 调整 blob 的大小
Azure Functions Resize blob with two output blobs
我有一个 Azure 函数可以调整图像大小,然后保存到另一个容器文件夹。
我的代码是;
private static readonly Dictionary<ImageSize, Tuple<int, int>> ImageDimensionsTable = new Dictionary<ImageSize, Tuple<int, int>>()
{
{ ImageSize.ExtraSmall, Tuple.Create(320, 200) },
{ ImageSize.Small, Tuple.Create(640, 400) },
{ ImageSize.Medium, Tuple.Create(800, 600) }
};
private enum ImageSize
{
/// <summary>
/// The extra small
/// </summary>
ExtraSmall,
/// <summary>
/// The small
/// </summary>
Small,
/// <summary>
/// The medium
/// </summary>
Medium
}
[FunctionName("Function1")]
public static void Run(
[BlobTrigger("vehicle-images/{folder}/{name}", Connection = "")]Stream image,
string name,
[Blob("watermarked-vehicle-images/{folder}/s-{name}", FileAccess.ReadWrite, Connection = "")]CloudBlockBlob imageSmall,
[Blob("watermarked-vehicle-images/{folder}/m-{name}", FileAccess.ReadWrite, Connection = "")]CloudBlockBlob imageMedium,
TraceWriter log)
{
// Resize and save Small Image
var size = ImageDimensionsTable[ImageSize.Small];
var outputStream = new MemoryStream();
var instructions = new Instructions
{
Width = size.Item1,
Height = size.Item2,
Mode = FitMode.Max,
OutputFormat = OutputFormat.Jpeg,
JpegQuality = 85
};
ImageBuilder.Current.Build(new ImageJob(image, outputStream, instructions));
outputStream.Seek(0, SeekOrigin.Begin);
imageSmall.Properties.ContentType = "image/jpeg";
imageSmall.UploadFromStream(outputStream);
// Resize and save Medium Image.
size = ImageDimensionsTable[ImageSize.Medium];
var outputStream2 = new MemoryStream();
var instructions2 = new Instructions
{
Width = size.Item1,
Height = size.Item2,
Mode = FitMode.Max,
OutputFormat = OutputFormat.Jpeg,
JpegQuality = 85
};
ImageBuilder.Current.Build(new ImageJob(image, outputStream2, instructions2));
outputStream2.Seek(0, SeekOrigin.Begin);
imageMedium.Properties.ContentType = "image/jpeg";
imageMedium.UploadFromStream(outputStream2);
}
因此,当前正在执行的操作是触发图片上传,执行将尺寸调整为小尺寸的第一步,但是当它到达将尺寸调整为中尺寸的代码时,出现错误Error: my-vehicle-image.jpg Parameter is not valid.
所以我怀疑我原来的 Stream image
被关闭了?或处理掉,这样当我开始尝试使用 ImageBuilder.Current.Build(new ImageJob(image, outputStream2, instructions2));
时,这是无效的?
希望这很容易解决。我的替代方法是使用 2 个函数,一个调整小的大小,一个调整大的大小。
为了缩放和其他目的,我推荐两个函数。在第一次调整大小后寻找源流可能是一个问题,具体取决于您用于输入流的绑定和技术。
我个人创建了一个类似的解决方案,我首先将源流读取到 MemoryStream,然后在 MemoryStream 上执行多个操作以解耦源流的能力(查找等)。
这可能会增加您的内存占用量,您可以再次尝试通过 RecyclableMemoryStream (https://github.com/Microsoft/Microsoft.IO.RecyclableMemoryStream)
等解决方案将其最小化
好的,所以发现 ImageJob
有一个接受 bool disposeSource
的重载,所以我将我的代码更新为;
ImageBuilder.Current.Build(new ImageJob(image, outputStream, instructions, false, false));
这样我第二次想用image
还是可以用
我有一个 Azure 函数可以调整图像大小,然后保存到另一个容器文件夹。
我的代码是;
private static readonly Dictionary<ImageSize, Tuple<int, int>> ImageDimensionsTable = new Dictionary<ImageSize, Tuple<int, int>>()
{
{ ImageSize.ExtraSmall, Tuple.Create(320, 200) },
{ ImageSize.Small, Tuple.Create(640, 400) },
{ ImageSize.Medium, Tuple.Create(800, 600) }
};
private enum ImageSize
{
/// <summary>
/// The extra small
/// </summary>
ExtraSmall,
/// <summary>
/// The small
/// </summary>
Small,
/// <summary>
/// The medium
/// </summary>
Medium
}
[FunctionName("Function1")]
public static void Run(
[BlobTrigger("vehicle-images/{folder}/{name}", Connection = "")]Stream image,
string name,
[Blob("watermarked-vehicle-images/{folder}/s-{name}", FileAccess.ReadWrite, Connection = "")]CloudBlockBlob imageSmall,
[Blob("watermarked-vehicle-images/{folder}/m-{name}", FileAccess.ReadWrite, Connection = "")]CloudBlockBlob imageMedium,
TraceWriter log)
{
// Resize and save Small Image
var size = ImageDimensionsTable[ImageSize.Small];
var outputStream = new MemoryStream();
var instructions = new Instructions
{
Width = size.Item1,
Height = size.Item2,
Mode = FitMode.Max,
OutputFormat = OutputFormat.Jpeg,
JpegQuality = 85
};
ImageBuilder.Current.Build(new ImageJob(image, outputStream, instructions));
outputStream.Seek(0, SeekOrigin.Begin);
imageSmall.Properties.ContentType = "image/jpeg";
imageSmall.UploadFromStream(outputStream);
// Resize and save Medium Image.
size = ImageDimensionsTable[ImageSize.Medium];
var outputStream2 = new MemoryStream();
var instructions2 = new Instructions
{
Width = size.Item1,
Height = size.Item2,
Mode = FitMode.Max,
OutputFormat = OutputFormat.Jpeg,
JpegQuality = 85
};
ImageBuilder.Current.Build(new ImageJob(image, outputStream2, instructions2));
outputStream2.Seek(0, SeekOrigin.Begin);
imageMedium.Properties.ContentType = "image/jpeg";
imageMedium.UploadFromStream(outputStream2);
}
因此,当前正在执行的操作是触发图片上传,执行将尺寸调整为小尺寸的第一步,但是当它到达将尺寸调整为中尺寸的代码时,出现错误Error: my-vehicle-image.jpg Parameter is not valid.
所以我怀疑我原来的 Stream image
被关闭了?或处理掉,这样当我开始尝试使用 ImageBuilder.Current.Build(new ImageJob(image, outputStream2, instructions2));
时,这是无效的?
希望这很容易解决。我的替代方法是使用 2 个函数,一个调整小的大小,一个调整大的大小。
为了缩放和其他目的,我推荐两个函数。在第一次调整大小后寻找源流可能是一个问题,具体取决于您用于输入流的绑定和技术。
我个人创建了一个类似的解决方案,我首先将源流读取到 MemoryStream,然后在 MemoryStream 上执行多个操作以解耦源流的能力(查找等)。
这可能会增加您的内存占用量,您可以再次尝试通过 RecyclableMemoryStream (https://github.com/Microsoft/Microsoft.IO.RecyclableMemoryStream)
等解决方案将其最小化好的,所以发现 ImageJob
有一个接受 bool disposeSource
的重载,所以我将我的代码更新为;
ImageBuilder.Current.Build(new ImageJob(image, outputStream, instructions, false, false));
这样我第二次想用image
还是可以用