我正在使用Magick.NET调整大小并将图像保存到AWS S3。我有一个Lambda无服务器Web API,它可以接收请求来执行此操作。在本地运行时,它可以正常工作,但在Lambda上调用实时终结点时则不能。我收到以下异常:
{
"Timestamp": "2018-08-31T09:26:40.2561647+00:00",
"Level": "Error",
"MessageTemplate": "### Error resizing image. ###",
"Exception": "ImageMagick.MagickMissingDelegateErrorException: NoDecodeDelegateForThisImageFormat `' @ error/blob.c/BlobToImage/456\n at TAP.Services.TourManagementService.<AddImage>d__24.MoveNext() in C:\\######\\TourManagementService.cs:line 180\n--- End of stack trace from previous location where exception was thrown ---\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\n at TAP.API.Tours.Controllers.ImagesController.<Post>d__3.MoveNext() in C:\\######\\ImagesController.cs:line 44\n--- End of stack trace from previous location where exception was thrown ---\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()\n--- End of stack trace from previous location where exception was thrown ---\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.MoveNext()\n--- End of stack trace from previous location where exception was thrown ---\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext()\n--- End of stack trace from previous location where exception was thrown ---\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextExceptionFilterAsync>d__23.MoveNext()",
"Properties": {
"ExceptionDetail": {
"HResult": -2146233088,
"Message": "NoDecodeDelegateForThisImageFormat `' @ error/blob.c/BlobToImage/456",
"Source": "Magick.NET-Q16-AnyCPU",
"RelatedExceptions": [
{
"RelatedExceptions": [],
"Message": "UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/714",
"Data": {},
"InnerException": null,
"TargetSite": null,
"StackTrace": null,
"HelpLink": null,
"Source": null,
"HResult": -2146233088
}
],
"Type": "ImageMagick.MagickMissingDelegateErrorException"
}
}
}
我要上传的图像在这里:https://wetransfer.com/downloads/6e86e0008b49ac29399c8cc2103c667820180831084315/99efdf,而我使用Magick.NET的代码如下:
private byte[] resizeImage(byte[] imageData, int width, int height)
{
using (var image = new MagickImage(imageData, new MagickReadSettings { Format = MagickFormat.Jpeg }))
{
image.Format = MagickFormat.Jpeg;
image.Interlace = Interlace.Jpeg; // Make image progressive
var geometry = new MagickGeometry(width, height);
geometry.FillArea = true;
try
{
image.Resize(geometry);
image.Crop(width, height);
image.RePage();
}
catch (Exception ex)
{
this.logger.Error(ex, "Error resizing image.");
throw new ImageResizeException("Error resizing image.", ex);
}
return image.ToByteArray();
}
}
在尝试从本地文件读取之前,我在使用Lambda时遇到问题,因此我认为UnableToOpenConfigureFile
可能是一个类似的问题。然后,我使用GitHub指南(https://github.com/dlemstra/Magick.NET/blob/master/Documentation/Initialization.md)通过代码而不是像下面那样依赖.xml文件来初始化MagickNET:
var configFiles = ConfigurationFiles.Default;
configFiles.Policy.Data = @"
<policymap>
<policy domain=""delegate"" rights=""none"" pattern=""*"" />
<policy domain=""coder"" rights=""none"" pattern=""*"" />
<policy domain=""coder"" rights=""read|write"" pattern=""{GIF,JPEG,PNG,WEBP}"" />
</policymap>";
MagickNET.Initialize(configFiles);
...但是可悲的是,这并没有改变任何东西,我仍然遇到相同的错误。正如我所说,这一切在本地都可以正常工作,这使我认为这是环境问题。
编辑:
通过执行此操作,我需要告诉Magick.NET在哪里找到文件,因此我尝试使用Lambda中的环境变量MAGICK_CONFIGURE_PATH
(已将其设置为“ / MagickNet”)进行处理。我尝试的另一种方法是像这样在MagickNET.Initialize
中设置路径:
MagickNET.Initialize("./MagickNet");
这也没有帮助我。