Wie man IronWord auf AWS Lambda verwendet
Dieser Artikel bietet eine umfassende Anleitung zum Einrichten einer AWS Lambda-Funktion mit IronWord. Sie lernen, wie Sie IronWord konfigurieren, um Word-Dokumente innerhalb einer AWS Lambda-Umgebung zu erstellen und zu bearbeiten.
Wie man IronWord auf AWS Lambda verwendet
- Laden Sie IronWord herunter und fügen Sie es Ihrem Projekt hinzu.
- Erstellen Sie ein AWS Lambda-Projekt mit Visual Studio
- Ändern Sie den FunctionHandler-Code, um Word-Dokumente zu generieren.
- Konfigurieren und stellen Sie das Projekt mit einem Docker-Container bereit.
- Rufen Sie die Lambda-Funktion auf und überprüfen Sie die Ausgabedateien auf S3.
Installation
Da dieses Beispiel Word-Dokumente in einen S3-Bucket lesen/schreiben wird, ist das AWSSDK.S3 NuGet-Paket erforderlich.
Verwendung von IronWord ZIP-Paket auf AWS Lambda
Beim Verwenden des IronWord ZIP-Pakets ist es wichtig, einen temporären Bereitstellungspfad festzulegen, da das Dateisystem von AWS Lambda schreibgeschützt ist, außer für den /tmp/-Ordner. Sie müssen IronWord so konfigurieren, dass es diesen Ordner für seine Laufzeitdateien verwendet:
var awsTmpPath = @"/tmp/";
IronSoftware.Word.Installation.DeploymentPath = awsTmpPath;
var awsTmpPath = @"/tmp/";
IronSoftware.Word.Installation.DeploymentPath = awsTmpPath;
Dim awsTmpPath = "/tmp/"
IronSoftware.Word.Installation.DeploymentPath = awsTmpPath
Integration von IronWord mit AWS
Erstellen Sie ein AWS Lambda-Projekt
Verwenden Sie Visual Studio, um ein containerisiertes AWS Lambda-Projekt zu erstellen:
- Installieren Sie das AWS Toolkit für Visual Studio
- Wählen Sie AWS Lambda Project (.NET Core - C#)
- Wählen Sie die .NET 8 (Container Image) Blaupause und beenden Sie das Setup
- Wählen Sie Container-Image als Bereitstellungstyp
Paketeabhängigkeiten hinzufügen
Fügen Sie IronWord- und AWSSDK.S3-Pakete über NuGet zu Ihrem Projekt hinzu. Die IronWord-Bibliothek funktioniert reibungslos auf AWS Lambda mit der richtigen Einrichtung. Führen Sie den folgenden Befehl aus, um IronWord nahtlos zu Ihrem AWS-Projekt hinzuzufügen:
Install-Package IronWord
Aktualisieren Sie die Dockerfile Ihres Projekts, um das .NET 8 Lambda-Basisimage zu verwenden und Ihre Build-Artefakte zu kopieren:
FROM public.ecr.aws/lambda/dotnet:8
RUN dnf update -y
WORKDIR /var/task
COPY "bin/Release/lambda-publish" .
Ändern des FunctionHandler-Codes
Unten ist eine Beispiel-Lambda-Funktion, die ein einfaches Word-Dokument erstellt, es als .docx-Datei speichert und in einen S3-Bucket hochlädt.
using Amazon.Lambda.Core;
using Amazon.S3;
using Amazon.S3.Model;
using IronWord;
using IronWord.Models;
using System.Text;
// 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 IronWordAwsLambda;
public class Function
{
private static readonly IAmazonS3 _s3Client = new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1);
public async Task FunctionHandler(ILambdaContext context)
{
var awsTmpPath = @"/tmp/";
License.LicenseKey = "YOUR-LICENSE-KEY"; // Replace if you have one
string filename = Guid.NewGuid() + ".docx";
string localPath = Path.Combine(awsTmpPath, filename);
string bucketName = "your-s3-bucket-name";
string objectKey = $"IronWordAwsLambda/{filename}";
try
{
// Create Word Document
var doc = new WordDocument();
Paragraph paragraph = new Paragraph(new TextContent("Hello from IronWord on AWS Lambda!"));
doc.AddParagraph(paragraph);
doc.SaveAs(localPath);
context.Logger.LogLine("Word document created.");
// Upload to S3
byte[] fileBytes = await File.ReadAllBytesAsync(localPath);
await UploadToS3Async(bucketName, objectKey, fileBytes);
context.Logger.LogLine($"Document uploaded to S3: {bucketName}/{objectKey}");
}
catch (Exception ex)
{
context.Logger.LogLine($"[ERROR] {ex.Message}");
}
}
private async Task UploadToS3Async(string bucketName, string objectKey, byte[] fileBytes)
{
using var stream = new MemoryStream(fileBytes);
var request = new PutObjectRequest
{
BucketName = bucketName,
Key = objectKey,
InputStream = stream,
ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
};
await _s3Client.PutObjectAsync(request);
}
}
using Amazon.Lambda.Core;
using Amazon.S3;
using Amazon.S3.Model;
using IronWord;
using IronWord.Models;
using System.Text;
// 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 IronWordAwsLambda;
public class Function
{
private static readonly IAmazonS3 _s3Client = new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1);
public async Task FunctionHandler(ILambdaContext context)
{
var awsTmpPath = @"/tmp/";
License.LicenseKey = "YOUR-LICENSE-KEY"; // Replace if you have one
string filename = Guid.NewGuid() + ".docx";
string localPath = Path.Combine(awsTmpPath, filename);
string bucketName = "your-s3-bucket-name";
string objectKey = $"IronWordAwsLambda/{filename}";
try
{
// Create Word Document
var doc = new WordDocument();
Paragraph paragraph = new Paragraph(new TextContent("Hello from IronWord on AWS Lambda!"));
doc.AddParagraph(paragraph);
doc.SaveAs(localPath);
context.Logger.LogLine("Word document created.");
// Upload to S3
byte[] fileBytes = await File.ReadAllBytesAsync(localPath);
await UploadToS3Async(bucketName, objectKey, fileBytes);
context.Logger.LogLine($"Document uploaded to S3: {bucketName}/{objectKey}");
}
catch (Exception ex)
{
context.Logger.LogLine($"[ERROR] {ex.Message}");
}
}
private async Task UploadToS3Async(string bucketName, string objectKey, byte[] fileBytes)
{
using var stream = new MemoryStream(fileBytes);
var request = new PutObjectRequest
{
BucketName = bucketName,
Key = objectKey,
InputStream = stream,
ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
};
await _s3Client.PutObjectAsync(request);
}
}
Imports Amazon.Lambda.Core
Imports Amazon.S3
Imports Amazon.S3.Model
Imports IronWord
Imports IronWord.Models
Imports System.Text
' 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 IronWordAwsLambda
Public Class [Function]
Private Shared ReadOnly _s3Client As IAmazonS3 = New AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1)
Public Async Function FunctionHandler(ByVal context As ILambdaContext) As Task
Dim awsTmpPath = "/tmp/"
License.LicenseKey = "YOUR-LICENSE-KEY" ' Replace if you have one
Dim filename As String = Guid.NewGuid().ToString() & ".docx"
Dim localPath As String = Path.Combine(awsTmpPath, filename)
Dim bucketName As String = "your-s3-bucket-name"
Dim objectKey As String = $"IronWordAwsLambda/{filename}"
Try
' Create Word Document
Dim doc = New WordDocument()
Dim paragraph As New Paragraph(New TextContent("Hello from IronWord on AWS Lambda!"))
doc.AddParagraph(paragraph)
doc.SaveAs(localPath)
context.Logger.LogLine("Word document created.")
' Upload to S3
Dim fileBytes() As Byte = Await File.ReadAllBytesAsync(localPath)
Await UploadToS3Async(bucketName, objectKey, fileBytes)
context.Logger.LogLine($"Document uploaded to S3: {bucketName}/{objectKey}")
Catch ex As Exception
context.Logger.LogLine($"[ERROR] {ex.Message}")
End Try
End Function
Private Async Function UploadToS3Async(ByVal bucketName As String, ByVal objectKey As String, ByVal fileBytes() As Byte) As Task
Dim stream = New MemoryStream(fileBytes)
Dim request = New PutObjectRequest With {
.BucketName = bucketName,
.Key = objectKey,
.InputStream = stream,
.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
}
Await _s3Client.PutObjectAsync(request)
End Function
End Class
End Namespace
Speicher und Timeout erhöhen
Da die Dokumentenverarbeitung speicherintensiv sein kann, erhöhen Sie den Speicher Ihrer Lambda-Funktion auf mindestens 512 MB und die Timeout auf 300 Sekunden in aws-lambda-tools-defaults.json:
{
"function-memory-size": 512,
"function-timeout": 300
}
Wenn Sie auf Fehler wie 'Runtime exited with error: signal: killed' stoßen, erhöhen Sie den Speicher oder optimieren Sie Ihren Code.
Veröffentlichung
Um Ihre Lambda-Funktion zu veröffentlichen:
- Klicken Sie mit der rechten Maustaste in Visual Studio auf Ihr Projekt
- Wählen Sie 'Veröffentlichen auf AWS Lambda...'
- Folgen Sie dem Assistenten und konfigurieren Sie die Einstellungen.
Probieren Sie es aus!
Rufen Sie die Lambda-Funktion über die AWS Lambda-Konsole oder Visual Studio auf. Nach der Ausführung überprüfen Sie Ihren S3-Bucket auf das neu erstellte Word-Dokument.
Häufig gestellte Fragen
Was ist IronWord und wie kann es die Verarbeitung von Word-Dokumenten auf AWS Lambda verbessern?
IronWord ist ein leistungsstarkes Tool zur Verarbeitung von Word-Dokumenten, und bei Integration mit AWS Lambda bietet es skalierbare und effiziente Dokumentenverwaltung, mit der Sie Aufgaben mit Word-Dokumenten automatisieren und optimieren können.
Wie integriere ich IronWord mit AWS Lambda?
Um IronWord mit AWS Lambda zu integrieren, müssen Sie das IronWord-Paket in Ihrer AWS Lambda-Umgebung einrichten, die erforderlichen Berechtigungen konfigurieren und Ihren Funktionscode bereitstellen, der die Funktionen von IronWord für die Dokumentenverarbeitung nutzt.
Welche Vorteile bietet die Nutzung von IronWord auf AWS Lambda?
Die Nutzung von IronWord auf AWS Lambda ermöglicht es Ihnen, die serverlose Architektur für die Verarbeitung von Word-Dokumenten zu nutzen, was Skalierbarkeit, Kosteneffizienz und reduziertes Infrastrukturmanagement bietet.
Kann ich Aufgaben mit Word-Dokumenten mithilfe von IronWord auf AWS Lambda automatisieren?
Ja, Sie können verschiedene Aufgaben mit Word-Dokumenten wie Erstellung, Änderung und Konvertierung mithilfe von IronWord innerhalb von AWS Lambda-Funktionen automatisieren.
Ist es möglich, große Word-Dokumente mit IronWord auf AWS Lambda zu verarbeiten?
IronWord ist darauf ausgelegt, große Word-Dokumente effizient zu verarbeiten, und bei Verwendung mit AWS Lambda kann es Dokumente in einem skalierbaren Maß verarbeiten, je nach Ihrer Lambda-Konfiguration.
Welche Arten von Word-Dokumentoperationen kann IronWord auf AWS Lambda durchführen?
IronWord kann eine Vielzahl von Operationen an Word-Dokumenten durchführen, einschließlich Bearbeitung, Formatierung, Textextraktion und Konvertierung in verschiedene Formate, alles in einer AWS Lambda-Umgebung.
Gibt es Voraussetzungen für die Bereitstellung von IronWord auf AWS Lambda?
Bevor Sie IronWord auf AWS Lambda bereitstellen, stellen Sie sicher, dass Sie über ein AWS-Konto verfügen, mit der Einrichtung von AWS Lambda vertraut sind und alle erforderlichen IAM-Rollen und Berechtigungen konfiguriert sind.
Wie handhabt IronWord Updates und Wartung auf AWS Lambda?
IronWord wird regelmäßig aktualisiert, um Kompatibilität und Leistungsverbesserungen zu gewährleisten. In einer AWS Lambda-Umgebung können Sie Ihr Bereitstellungspaket ganz einfach mit der neuesten Version von IronWord aktualisieren, um die optimale Funktionalität aufrechtzuerhalten.
Welche Unterstützung gibt es für die Nutzung von IronWord mit AWS Lambda?
Iron Software bietet Dokumentation und Unterstützung, um Benutzern bei der Integration und Nutzung von IronWord auf AWS Lambda zu helfen, sodass Sie seine Fähigkeiten effektiv für Ihre Dokumentverarbeitungsbedürfnisse nutzen können.

