IronQR 開始使用 在 AWS 上設置 如何在 AWS Lambda 上讀取和寫入二維碼 Curtis Chau 更新:2025年7月22日 下載 IronQR NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 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 本文提供了使用 IronQR 設定 AWS Lambda 函數的詳細指南。 在本教程中,您將學習如何配置 IronQR,以便直接向 S3 儲存桶讀取和寫入二維碼。 ## 如何在 AWS Lambda 上讀取和寫入二維碼 下載用於讀寫二維碼的 C# 庫 創建 AWS Lambda 容器化專案模板 修改 Dockerfile 和 FunctionHandler 代碼 增加記憶體和超時設置 部署並調用函數以在 S3 中查看結果 安裝 本文將使用 S3 儲存桶,因此需要AWSSDK.S3包。 !{--01001100010010010100001001010010010000010101001001011001010111110101001101010100010001010101010 10100010111110101010001010010010010010100000101001100010111110100001001001100010011111010000100100110001001111010101 建立 AWS Lambda 項目 使用 Visual Studio,建立容器化的 AWS Lambda 函數非常簡單: 安裝適用於 Visual Studio 的 AWS 工具包 選擇"AWS Lambda 專案(.NET Core - C#)" 選擇".NET 8(容器映像)"藍圖,然後選擇"完成"。 !選擇容器影像 新增包依賴項 .NET 8 中的 IronQR 函式庫可在 AWS Lambda 上執行,無需額外的相依性。 若要進行配置,請如下所示修改專案的 Dockerfile: FROM public.ecr.aws/lambda/dotnet:8 # Install necessary packages and update repositories 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" . 修改函數處理程序程式碼 此範例產生一個二維碼,將其上傳到 S3 儲存桶,並讀取新產生的二維碼。 檔案路徑在 IronQrNuget 目錄中指定,檔案名稱使用全域唯一識別碼 (GUID)。 Write方法根據提供的值產生二維碼,然後將產生的 JPG 位元組數組傳遞給Read方法以讀取二維碼。 這證明該 AWS Lambda 函數能夠讀取二維碼。 using Amazon.Lambda.Core; using Amazon.S3; using Amazon.S3.Model; using IronQr; // 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 IronQrNuGetAwsLambda { public class Function { private static readonly IAmazonS3 _s3Client = new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1); /// <summary> /// Main handler for AWS Lambda /// </summary> /// <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param> /// <returns></returns> public async Task FunctionHandler(ILambdaContext context) { // Set the license key for IronQR IronQr.License.LicenseKey = "IronQR-MYLICENSE-KEY-1EF01"; string bucketName = "deploymenttestbucket"; string objectKey = $"IronQrNuget/{Guid.NewGuid()}.png"; try { // Create a QR code with the content "12345" var myQr = QrWriter.Write("12345"); context.Logger.LogLine("QR created."); // Upload the JPG to S3 await UploadJpgToS3Async(bucketName, objectKey, myQr.Save().ExportBytesAsJpg()); context.Logger.LogLine($"QR uploaded successfully to {bucketName}/{objectKey}"); // Read the QR code QrImageInput imageInput = new QrImageInput(myQr.Save()); QrReader reader = new QrReader(); var resultFromByte = reader.Read(imageInput); foreach (var item in resultFromByte) { // Log the read value context.Logger.LogLine($"QR value is = {item.Value}"); } } catch (Exception e) { context.Logger.LogLine($"[ERROR] FunctionHandler: {e.Message}"); } } // Function to upload the JPG file to S3 private async Task UploadJpgToS3Async(string bucketName, string objectKey, byte[] jpgBytes) { using (var memoryStream = new MemoryStream(jpgBytes)) { var request = new PutObjectRequest { BucketName = bucketName, Key = objectKey, InputStream = memoryStream, ContentType = "image/jpg", }; await _s3Client.PutObjectAsync(request); } } } } using Amazon.Lambda.Core; using Amazon.S3; using Amazon.S3.Model; using IronQr; // 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 IronQrNuGetAwsLambda { public class Function { private static readonly IAmazonS3 _s3Client = new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1); /// <summary> /// Main handler for AWS Lambda /// </summary> /// <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param> /// <returns></returns> public async Task FunctionHandler(ILambdaContext context) { // Set the license key for IronQR IronQr.License.LicenseKey = "IronQR-MYLICENSE-KEY-1EF01"; string bucketName = "deploymenttestbucket"; string objectKey = $"IronQrNuget/{Guid.NewGuid()}.png"; try { // Create a QR code with the content "12345" var myQr = QrWriter.Write("12345"); context.Logger.LogLine("QR created."); // Upload the JPG to S3 await UploadJpgToS3Async(bucketName, objectKey, myQr.Save().ExportBytesAsJpg()); context.Logger.LogLine($"QR uploaded successfully to {bucketName}/{objectKey}"); // Read the QR code QrImageInput imageInput = new QrImageInput(myQr.Save()); QrReader reader = new QrReader(); var resultFromByte = reader.Read(imageInput); foreach (var item in resultFromByte) { // Log the read value context.Logger.LogLine($"QR value is = {item.Value}"); } } catch (Exception e) { context.Logger.LogLine($"[ERROR] FunctionHandler: {e.Message}"); } } // Function to upload the JPG file to S3 private async Task UploadJpgToS3Async(string bucketName, string objectKey, byte[] jpgBytes) { using (var memoryStream = new MemoryStream(jpgBytes)) { var request = new PutObjectRequest { BucketName = bucketName, Key = objectKey, InputStream = memoryStream, ContentType = "image/jpg", }; await _s3Client.PutObjectAsync(request); } } } } Imports Amazon.Lambda.Core Imports Amazon.S3 Imports Amazon.S3.Model Imports IronQr ' 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 IronQrNuGetAwsLambda Public Class [Function] Private Shared ReadOnly _s3Client As IAmazonS3 = New AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1) ''' <summary> ''' Main handler for AWS Lambda ''' </summary> ''' <param name="context">The ILambdaContext that provides methods for logging and describing the Lambda environment.</param> ''' <returns></returns> Public Async Function FunctionHandler(ByVal context As ILambdaContext) As Task ' Set the license key for IronQR IronQr.License.LicenseKey = "IronQR-MYLICENSE-KEY-1EF01" Dim bucketName As String = "deploymenttestbucket" Dim objectKey As String = $"IronQrNuget/{Guid.NewGuid()}.png" Try ' Create a QR code with the content "12345" Dim myQr = QrWriter.Write("12345") context.Logger.LogLine("QR created.") ' Upload the JPG to S3 Await UploadJpgToS3Async(bucketName, objectKey, myQr.Save().ExportBytesAsJpg()) context.Logger.LogLine($"QR uploaded successfully to {bucketName}/{objectKey}") ' Read the QR code Dim imageInput As New QrImageInput(myQr.Save()) Dim reader As New QrReader() Dim resultFromByte = reader.Read(imageInput) For Each item In resultFromByte ' Log the read value context.Logger.LogLine($"QR value is = {item.Value}") Next item Catch e As Exception context.Logger.LogLine($"[ERROR] FunctionHandler: {e.Message}") End Try End Function ' Function to upload the JPG file to S3 Private Async Function UploadJpgToS3Async(ByVal bucketName As String, ByVal objectKey As String, ByVal jpgBytes() As Byte) As Task Using memoryStream As New MemoryStream(jpgBytes) Dim request = New PutObjectRequest With { .BucketName = bucketName, .Key = objectKey, .InputStream = memoryStream, .ContentType = "image/jpg" } Await _s3Client.PutObjectAsync(request) End Using End Function End Class End Namespace $vbLabelText $csharpLabel 增加記憶體和超時 Lambda 函數的記憶體分配取決於文件的大小和同時處理的文件數量。 首先,在aws-lambda-tools-defaults.json中將記憶體設定為 512 MB,逾時設定為 300 秒。 "function-memory-size" : 512, "function-timeout" : 300 如果記憶體不足,程式將引發錯誤:"運行時錯誤退出:訊號:已終止"。增加記憶體大小可以幫助解決此問題。 如需進一步指導,請查看故障排除文章: AWS Lambda - 執行時間退出訊號:已終止。 發布 要在 Visual Studio 中發布,只需右鍵單擊項目,然後選擇"發佈到 AWS Lambda..." 然後,配置所需的設定。 如需了解更多信息,請訪問AWS 網站。 試試看! 您可以透過Lambda 控制台或 Visual Studio 啟動 Lambda 函數。 常見問題解答 如何將 QR 碼庫集成到我的 C# 項目中在 AWS Lambda 上? 您可以通過下載 IronQR 庫,在 Visual Studio 中創建容器化的 AWS Lambda 項目,並配置您的環境以支持 QR 碼操作,將像 IronQR 這樣的 QR 碼庫集成到您的 C# 項目中在 AWS Lambda 上。 配置 IronQR 用於 AWS Lambda 的步驟是什麼? 通過在 Visual Studio 中設置容器化專案模板,修改 Dockerfile,更新 FunctionHandler 並調整記憶體和超時設置來配置 IronQR 用於 AWS Lambda 以確保穩定運行。 如何使用 C# 庫管理與 AWS S3 的 QR 碼? 使用 IronQR,您可以透過創建、上傳到和從 AWS S3 儲存桶中讀取來管理 QR 碼。這涉及將 IronQR 庫與 AWSSDK.S3 套件結合使用,以處理與 S3 儲存桶的操作。 如果我使用 IronQR 的 AWS Lambda 函數運行不佳,我應該怎麼做? 如果您的 AWS Lambda 函數運行不佳,請考慮在您的 aws-lambda-tools-defaults.json 文件中增加記憶體和超時設置以分配更多資源給函數。 如何將 C# Lambda 函數部署到 AWS? 通過 Visual Studio 的 "發布到 AWS Lambda..." 選項部署 C# Lambda 函數到 AWS,配置必要的設置,並利用 AWS Toolkit 進行部署。 是否可以在 Visual Studio 中測試我的 AWS Lambda 函數? 是的,您可以通過使用 AWS Toolkit 在 Visual Studio 中調用函數並直接在開發環境中驗證其輸出來測試 AWS Lambda 函數。 如何排除我的 AWS Lambda 函數的記憶體問題? 要排除記憶體問題,請增加 aws-lambda-tools-defaults.json 文件中的函數記憶體分配,並在重新部署後監控函數的性能。 在 AWS Lambda 項目中修改 Dockerfile 的重要性是什麼? 在 AWS Lambda 項目中修改 Dockerfile 對於正確設置環境至關重要,包括更新存儲庫和複製必要的構建工件以確保函數正確執行。 如何確保我的 AWS Lambda 函數有效處理 QR 碼操作? 確保您的 AWS Lambda 函數有效處理 QR 碼操作的方法是優化記憶體和超時設置,使用像 IronQR 這樣的合適庫,並正確配置 Dockerfile 和專案設置。 我可以在 AWS Lambda 中自動生成和檢索 QR 碼嗎? 是的,您可以使用 IronQR 在 AWS Lambda 中自動生成和檢索 QR 碼。該庫允許您以編程方式創建 QR 碼,將其上傳到 S3 並根據需要讀回。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 準備好開始了嗎? Nuget 下載 58,270 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:58,270 查看許可證