IronBarcode 開始使用 在 AWS 上設置 如何在 AWS Lambda 上讀取和寫入條碼 Curtis Chau 更新:2025年7月22日 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English 本文提供了使用 IronBarcode 設定 AWS Lambda 函數的全面指南。 在本指南中,您將學習如何配置 IronBarcode 以從 S3 儲存桶讀取條碼並將其寫入條碼。 ## 如何在 AWS Lambda 上讀取和寫入條碼 下載用於讀寫條碼的 C# 庫 創建並選擇項目模板 修改 FunctionHandler 代碼 配置並部署項目 調用函數並檢查 S3 中的結果 安裝 本文將使用 S3 儲存桶,因此需要AWSSDK.S3包。 使用 IronBarcode Zip 如果您使用的是 IronBarcode ZIP,則必須設定臨時資料夾。 var awsTmpPath = @"/tmp/"; IronBarCode.Installation.DeploymentPath = awsTmpPath; var awsTmpPath = @"/tmp/"; IronBarCode.Installation.DeploymentPath = awsTmpPath; Dim awsTmpPath = "/tmp/" IronBarCode.Installation.DeploymentPath = awsTmpPath $vbLabelText $csharpLabel 讀取條碼需要Microsoft.ML.OnnxRuntime程式包。 雖然無需機器學習 (ML) 即可寫入條碼,但讀取條碼的預設模式依賴於機器學習 (ML)。 如果切換到不使用 ML 的閱讀模式,則不需要該軟體包。 建立 AWS Lambda 項目 使用 Visual Studio,建立容器化的 AWS Lambda 函數非常簡單: 安裝適用於 Visual Studio 的 AWS 工具包 選擇"AWS Lambda 專案(.NET Core - C#)" 選擇".NET 8(容器映像)"藍圖,然後選擇"完成"。 !選擇容器影像 新增包依賴項 .NET 8 中的 IronBarcode 庫無需額外相依性即可在 AWS Lambda 上運作。 若要進行設置,請如下更新專案的 Dockerfile: FROM public.ecr.aws/lambda/dotnet:8 # Install necessary packages RUN dnf update -y WORKDIR /var/task # This COPY command copies the .NET Lambda project's build artifacts from the host machine into the image. # The source of the COPY should match where the .NET Lambda project publishes its build artifacts. If the Lambda function is being built # with the AWS .NET Lambda Tooling, the `--docker-host-build-output-dir` switch controls where the .NET Lambda project # will be built. The .NET Lambda project templates default to having `--docker-host-build-output-dir` # set in the aws-lambda-tools-defaults.json file to "bin/Release/lambda-publish". # # Alternatively Docker multi-stage build could be used to build the .NET Lambda project inside the image. # For more information on this approach check out the project's README.md file. COPY "bin/Release/lambda-publish" . 修改函數處理程序程式碼 此範例產生 EAN8 條碼,將其上傳到 S3 儲存桶,並讀取新建立的條碼。 使用 IronBarcode ZIP 時,配置臨時資料夾至關重要,因為該程式庫需要寫入權限才能從 DLL 複製執行時間資料夾。 using Amazon.Lambda.Core; using Amazon.S3; using Amazon.S3.Model; using IronBarCode; // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace IronBarcodeZipAwsLambda; public class Function { private static readonly IAmazonS3 _s3Client = new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1); /// <summary> /// AWS Lambda Function Handler. It generates a barcode, uploads it to S3, and then reads it. /// </summary> /// <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param> public async Task FunctionHandler(ILambdaContext context) { var awsTmpPath = @"/tmp/"; IronBarCode.Installation.DeploymentPath = awsTmpPath; // Set your IronBarcode license key here IronBarCode.License.LicenseKey = "IRONBARCODE-MYLICENSE-KEY-1EF01"; string filename = Guid.NewGuid().ToString(); string bucketName = "deploymenttestbucket"; string objectKey = $"IronBarcodeZip/{filename}.png"; try { // Creating a barcode with EAN8 encoding var myBarcode = BarcodeWriter.CreateBarcode("1212345", BarcodeWriterEncoding.EAN8); context.Logger.LogLine($"Barcode created."); // Upload the PNG image of the barcode to the specified S3 bucket await UploadPngToS3Async(bucketName, objectKey, myBarcode.ToPngBinaryData()); context.Logger.LogLine($"Barcode uploaded successfully to {bucketName}/{objectKey}"); // Read and log the barcode value from the PNG binary data var resultFromByte = BarcodeReader.Read(myBarcode.ToPngBinaryData()); foreach (var item in resultFromByte) { context.Logger.LogLine($"Barcode value is = {item.Value}"); } } catch (Exception e) { context.Logger.LogLine($"[ERROR] FunctionHandler: {e.Message}"); } } /// <summary> /// Uploads a PNG byte array to the specified S3 bucket. /// </summary> /// <param name="bucketName">The name of the S3 bucket.</param> /// <param name="objectKey">The object key for the uploaded file in the bucket.</param> /// <param name="pdfBytes">Byte array of the PNG to be uploaded.</param> private async Task UploadPngToS3Async(string bucketName, string objectKey, byte[] pdfBytes) { using (var memoryStream = new MemoryStream(pdfBytes)) { var request = new PutObjectRequest { BucketName = bucketName, Key = objectKey, InputStream = memoryStream, ContentType = "image/png", }; await _s3Client.PutObjectAsync(request); } } } using Amazon.Lambda.Core; using Amazon.S3; using Amazon.S3.Model; using IronBarCode; // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace IronBarcodeZipAwsLambda; public class Function { private static readonly IAmazonS3 _s3Client = new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1); /// <summary> /// AWS Lambda Function Handler. It generates a barcode, uploads it to S3, and then reads it. /// </summary> /// <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param> public async Task FunctionHandler(ILambdaContext context) { var awsTmpPath = @"/tmp/"; IronBarCode.Installation.DeploymentPath = awsTmpPath; // Set your IronBarcode license key here IronBarCode.License.LicenseKey = "IRONBARCODE-MYLICENSE-KEY-1EF01"; string filename = Guid.NewGuid().ToString(); string bucketName = "deploymenttestbucket"; string objectKey = $"IronBarcodeZip/{filename}.png"; try { // Creating a barcode with EAN8 encoding var myBarcode = BarcodeWriter.CreateBarcode("1212345", BarcodeWriterEncoding.EAN8); context.Logger.LogLine($"Barcode created."); // Upload the PNG image of the barcode to the specified S3 bucket await UploadPngToS3Async(bucketName, objectKey, myBarcode.ToPngBinaryData()); context.Logger.LogLine($"Barcode uploaded successfully to {bucketName}/{objectKey}"); // Read and log the barcode value from the PNG binary data var resultFromByte = BarcodeReader.Read(myBarcode.ToPngBinaryData()); foreach (var item in resultFromByte) { context.Logger.LogLine($"Barcode value is = {item.Value}"); } } catch (Exception e) { context.Logger.LogLine($"[ERROR] FunctionHandler: {e.Message}"); } } /// <summary> /// Uploads a PNG byte array to the specified S3 bucket. /// </summary> /// <param name="bucketName">The name of the S3 bucket.</param> /// <param name="objectKey">The object key for the uploaded file in the bucket.</param> /// <param name="pdfBytes">Byte array of the PNG to be uploaded.</param> private async Task UploadPngToS3Async(string bucketName, string objectKey, byte[] pdfBytes) { using (var memoryStream = new MemoryStream(pdfBytes)) { var request = new PutObjectRequest { BucketName = bucketName, Key = objectKey, InputStream = memoryStream, ContentType = "image/png", }; await _s3Client.PutObjectAsync(request); } } } Imports Amazon.Lambda.Core Imports Amazon.S3 Imports Amazon.S3.Model Imports IronBarCode ' Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. <Assembly: LambdaSerializer(GetType(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))> Namespace IronBarcodeZipAwsLambda Public Class [Function] Private Shared ReadOnly _s3Client As IAmazonS3 = New AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1) ''' <summary> ''' AWS Lambda Function Handler. It generates a barcode, uploads it to S3, and then reads it. ''' </summary> ''' <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param> Public Async Function FunctionHandler(ByVal context As ILambdaContext) As Task Dim awsTmpPath = "/tmp/" IronBarCode.Installation.DeploymentPath = awsTmpPath ' Set your IronBarcode license key here IronBarCode.License.LicenseKey = "IRONBARCODE-MYLICENSE-KEY-1EF01" Dim filename As String = Guid.NewGuid().ToString() Dim bucketName As String = "deploymenttestbucket" Dim objectKey As String = $"IronBarcodeZip/{filename}.png" Try ' Creating a barcode with EAN8 encoding Dim myBarcode = BarcodeWriter.CreateBarcode("1212345", BarcodeWriterEncoding.EAN8) context.Logger.LogLine($"Barcode created.") ' Upload the PNG image of the barcode to the specified S3 bucket Await UploadPngToS3Async(bucketName, objectKey, myBarcode.ToPngBinaryData()) context.Logger.LogLine($"Barcode uploaded successfully to {bucketName}/{objectKey}") ' Read and log the barcode value from the PNG binary data Dim resultFromByte = BarcodeReader.Read(myBarcode.ToPngBinaryData()) For Each item In resultFromByte context.Logger.LogLine($"Barcode value is = {item.Value}") Next item Catch e As Exception context.Logger.LogLine($"[ERROR] FunctionHandler: {e.Message}") End Try End Function ''' <summary> ''' Uploads a PNG byte array to the specified S3 bucket. ''' </summary> ''' <param name="bucketName">The name of the S3 bucket.</param> ''' <param name="objectKey">The object key for the uploaded file in the bucket.</param> ''' <param name="pdfBytes">Byte array of the PNG to be uploaded.</param> Private Async Function UploadPngToS3Async(ByVal bucketName As String, ByVal objectKey As String, ByVal pdfBytes() As Byte) As Task Using memoryStream As New MemoryStream(pdfBytes) Dim request = New PutObjectRequest With { .BucketName = bucketName, .Key = objectKey, .InputStream = memoryStream, .ContentType = "image/png" } Await _s3Client.PutObjectAsync(request) End Using End Function End Class End Namespace $vbLabelText $csharpLabel 在 try 程式碼區塊之前,檔案目標位置設定為 IronBarcodeZip 目錄,檔案名稱由全域唯一識別碼 (GUID) 產生。 CreateBarcode方法用於產生條碼。 之後,將 PNG 位元組數組傳遞給Read方法來讀取條碼。 這證明 AWS Lambda 函數能夠讀取條碼。 Read方法也接受一個BarcodeReaderOptions對象,您可以對其進行自訂以啟用讀取多個條碼、定位特定區域、使用非同步和多執行緒處理、套用影像校正濾鏡等功能。 增加記憶體和超時 Lambda 函數中分配的記憶體量將根據正在處理的文件的大小和同時處理的文件數量而變化。 作為基準,在aws-lambda-tools-defaults.json中將記憶體設為 512 MB,逾時設為 300 秒。 { "function-memory-size": 512, "function-timeout": 300 } 當記憶體不足時,程式會拋出錯誤:"運行時錯誤退出:訊號:終止"。增加記憶體大小可以解決此問題。 如需更多詳細信息,請參閱故障排除文章: AWS Lambda - 運行時退出訊號:已終止。 發布 若要在 Visual Studio 中發布,請右鍵按一下專案並選擇"發佈到 AWS Lambda...",然後配置必要的設定。 您可以在AWS 網站上閱讀更多關於發布 Lambda 函數的資訊。 試試看! 您可以透過Lambda 控制台或 Visual Studio 啟動 Lambda 函數。 常見問題解答 如何在 AWS Lambda 上設置條碼讀取和寫入? 要在 AWS Lambda 上設置條碼讀取和寫入,使用 IronBarcode 庫,通過 Visual Studio 下載並創建 AWS Lambda 項目。修改 FunctionHandler 代碼以處理條碼操作,配置項目設置並部署。確保包含必要的包依賴,例如 AWSSDK.S3。 在 AWS Lambda 上進行條碼處理需要哪些包? 對於 AWS Lambda 上的條碼處理,包含用於 S3 互動的 AWSSDK.S3 包,以及可選的 Microsoft.ML.OnnxRuntime 包以進行高級機器學習條碼讀取,除非使用標準條碼讀取方法則可能不需要。 如何在 AWS Lambda 中修改 FunctionHandler 的條碼任務代碼? 修改 FunctionHandler 代碼以使用 IronBarcode 創建和讀取條碼。確保您設置了 IronBarcode 授權密鑰並配置臨時文件夾以正確運行 IronBarcode ZIP 文檔。 如何增加處理條碼的 AWS Lambda 函數的內存和超時時間? 調整 aws-lambda-tools-defaults.json 文件中的內存和超時設置,將 function-memory-size 設置為 512 MB,function-timeout 設置為 300 秒,以滿足 IronBarcode 的條碼處理需求。 如何用 Visual Studio 發佈一個用於條碼處理的 Lambda 函數? 通過右鍵點擊項目並選擇 'Publish to AWS Lambda...' 在 Visual Studio 中發佈 Lambda 函數,配置設置。遵循 AWS 文檔以了解詳細步驟,確保 IronBarcode 設置正確。 如何測試部署的 AWS Lambda 函數以進行條碼操作? 通過 AWS Lambda 控制台或直接從 Visual Studio 激活來測試 Lambda 函數,確保 IronBarcode 正確運行且條碼處理任務如預期執行。 在 AWS Lambda 上設置條碼處理中 Dockerfile 的角色是什麼? Dockerfile 幫助更新必要的包並將 .NET Lambda 項目的構建工件複製到鏡像中,以便於在 AWS Lambda 上使用 IronBarcode 進行無縫條碼處理。 為什麼在 AWS Lambda 上使用條碼庫時設置臨時文件夾很重要? 設置臨時文件夾是必須的,因為 IronBarcode 需要寫入權限才能進行 ZIP 操作,確保運行時文件夾從 DLL 中正確複製以便於在 AWS Lambda 上順利運行。 我可以在 AWS Lambda 上自定義條碼讀取嗎? 是的,IronBarcode 允許使用 BarcodeReaderOptions 來定制條碼讀取,包括讀取多個條碼、定位特定區域、啟用異步處理,以及應用圖像校正過濾器。 如果在條碼處理期間遇到 'Runtime exited with error: signal: killed' 錯誤,我應該怎麼做? 此錯誤表明內存分配不足。增加 aws-lambda-tools-defaults.json 文件中 Lambda 函數的內存來解決此問題,同時使用 IronBarcode。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 準備好開始了嗎? Nuget 下載 2,070,733 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:2,070,733 查看許可證