首页 > 解决方案 > 使用 .Net Core 2.0 编写的 AWS Lambda 函数无法加载 libdl

问题描述

我正在使用 Visual Studio 2017、AWS Toolkit for VS2017 和 AWS Lambda Project .Net Core 2.0 模板开发 c# lambda 函数。我已经成功开发和部署了一些功能。我开发了一个使用 Aspose.Net 18.5 版的函数。在我的 Windows 机器上测试时,该函数按预期执行。但是,当部署在 AWS Lambda 上时,我会收到以下堆栈跟踪:

One or more errors occurred. (The type initializer for 'Gdip' threw an exception.): AggregateException
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at lambda_method(Closure , Stream , Stream , LambdaContextInternal )

at System.Drawing.SafeNativeMethods.Gdip.GdipCreateMatrix2(Single m11, Single m12, Single m21, Single m22, Single dx, Single dy, IntPtr& matrix)
at \u0006   .\u0002(Single \u0002, Single \u0003, Single \u0005, Single , Single \u0006, Single \u000e, \u000e    \u000f)
at    .\u0002(\u0005    \u0002, \u000f ​  \u0003, \u000e    \u0005, Single , Single \u0006, Boolean \u000e, Int32 \u000f, Boolean \u0002 , Double& \u0003 , Double& \u0005 , \u0002 ​ &  )
at    ..ctor(\u000f ​  \u0002, \u0005    \u0003, \u000f ​  \u0005)
at \u0002   .\u000f     \u0002(\u000f ​  \u0002, \u0005    \u0003, \u000f ​  \u0005,    & )
at \u0006   .\u0002(   & \u0002)
at Aspose.Pdf.Devices.ImageDevice.\u0002(Page \u0002)
at Aspose.Pdf.Devices.PngDevice.Process(Page page, Stream output)
at CreateImagesFromPDF.Function.<FunctionHandler>d__10.MoveNext() in C:\Users\Thomas\source\repos\CreateImagesFromPDF\CreateImagesFromPDF\Function.cs:line 156
Unable to load DLL 'libdl': The specified module or one of its dependencies could not be found.
(Exception from HRESULT: 0x8007007E): DllNotFoundException
at Interop.Libdl.dlopen(String fileName, Int32 flag)
at System.Drawing.SafeNativeMethods.Gdip.LoadNativeLibrary()
at System.Drawing.SafeNativeMethods.Gdip..cctor()

这里的关键信息似乎是“无法加载 DLL 'libdl'。研究这个问题我发现 Aspose 依赖于 System.Drawing.Common,它依赖于 Windows 上的 libdl.dll 和 Linux 上的 libdl.so(Lambda 执行的地方) ). 显然,在 Linux 的某些发行版中,这个库是 libdl.so.2。如果这是一个常规应用程序,而不是 Lambda 函数,答案似乎是创建一个从 libdl.so.2 到 libdl 的符号链接。所以。不幸的是,我不相信我可以使用 Lambda。如果我在 Linux 上创建我的 Lambda 包,我会在包中包含重命名为 libdl.so 的 libdl.so.2。但是,我正在创建使用 AWS Toolkit 进行打包,但不知道如何将该库添加到其中。我将不胜感激任何可以解决此问题的帮助。

这是我的源代码:

using (GetObjectResponse response = await S3Client.GetObjectAsync(request))
{
    using (Stream responseStream = response.ResponseStream)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            responseStream.CopyTo(ms);

            using (Document doc = new Document(ms))
            {
                for (int i = 0; i < doc.Pages.Count; i++)
                {
                    string outFile = fileName + "_" + (i + 1).ToString() + ".png";
                    string tmpFile = "/tmp/" + outFile;
                    Page page = doc.Pages[i + 1];

                    using (FileStream imageStream = new FileStream(tmpFile, FileMode.Create))
                    {
                        PngDevice pngDevice;
                        pngDevice = new PngDevice(Convert.ToInt32((double)page.Rect.Width * 4.17), Convert.ToInt32((double)page.Rect.Height * 4.17));
                        pngDevice.Process(page, imageStream);
                        imageStream.Close();

                        var putRequest = new PutObjectRequest
                        {
                            BucketName = s3Event.Bucket.Name,
                            Key = newFolder + outFile,
                            ContentType = "image/png",
                            FilePath = tmpFile
                        };

                        PutObjectResponse resp = await S3Client.PutObjectAsync(putRequest);

                        File.Delete(tmpFile);

                    }
                }
            }
        }
    }
}

代码在“pngDevice.Process(page, imageStream);”行上失败。

标签: c#amazon-web-services.net-coreaws-lambdaaspose.pdf

解决方案


推荐阅读