将 Aspose.OCR 与 Amazon S3 集成,使开发人员能够有效地利用云存储,以便可视性字符识别(OCR)的结果。 这种整合不仅简化了 OCR 数据的管理,而且还提高了可扩展性和可访问性。 在本教程中,我们将通过设置 ASPOSE.OKR 的过程,与 AWS S 3 无缝工作,提供详细的示例和最佳实践。
完整的例子
原則
- 安装了 .NET 8 (或 .Net 6+) SDK。
- AWS 帐户与 Amazon S3 访问。
- 一个布克(例如,
my-ocr-demo-bucket
)在您最喜欢的区域(下面的例子使用ap-south-1
). - (可选) 如果您想要超越评估运行,请删除许可文件。
步骤 1: 为 .NET 设置 Aspose.OCR
安装一个新的控制台应用程序,并添加 NuGet 包。
# Create project
dotnet new console -n OcrS3Demo -f net8.0
cd OcrS3Demo
# Add Aspose.OCR (CPU) OR Aspose.OCR-GPU (pick exactly one)
dotnet add package Aspose.OCR
# dotnet add package Aspose.OCR-GPU # if you prefer GPU build
# Add AWS S3 SDK
dotnet add package AWSSDK.S3
阿斯波斯提供两 Aspose.OCR
(CPU)和 Aspose.OCR-GPU
通过 NuGet 包装;你只需要一个。Aspose 文档)
步骤2:为 .NET 设置 AWS SDK
设置您的 AWS 个人资料并创建一个漏洞(如果您已经拥有一个)。
# Configure credentials (creates ~/.aws/credentials and config)
aws configure --profile ocr-s3
# AWS Access Key ID: AKIA****************
# AWS Secret Access Key: ************************
# Default region name: ap-south-1
# Default output format: json
# Create a bucket in that region (bucket name must be globally unique)
aws s3api create-bucket \
--bucket my-ocr-demo-bucket \
--region ap-south-1 \
--create-bucket-configuration LocationConstraint=ap-south-1
推荐的最低 IAM 政策(加入您的用户/角色)为此教程:
{
"Version": "2012-10-17",
"Statement": [
{ "Effect": "Allow", "Action": ["s3:ListBucket"], "Resource": "arn:aws:s3:::my-ocr-demo-bucket" },
{ "Effect": "Allow", "Action": ["s3:GetObject", "s3:PutObject"], "Resource": "arn:aws:s3:::my-ocr-demo-bucket/*" }
]
}
AWS SDK for .NET 使用默认认链;设置 AWS_PROFILE=ocr-s3
Core S3 模式(创建、上传、下载)在 AWS 官方 .NET 示例中文档。AWS 文档)
步骤3:启动 Aspose.OCR API
创建一个基本 Program.cs
我们还将设置英语作为语言和文档布局检测(下面显示的所有类型来自当前的Aspose.OCR API表面)。参考.aspose.com)
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Aspose.OCR;
class Program
{
static async Task Main(string[] args)
{
// Optional: load license if you have one
// new License().SetLicense("Aspose.Total.lic");
var ocr = new AsposeOcr();
var settings = new RecognitionSettings
{
// pick your language(s); can combine if needed
Language = Language.Eng,
DetectAreasMode = DetectAreasMode.DOCUMENT
};
// We'll fill in S3 + OCR steps next...
}
}
关键 API 我们将使用下一个:
AsposeOcr.RecognizeImage(MemoryStream, RecognitionSettings)
返回 ARecognitionResult
.RecognitionResult.RecognitionText
/GetJson(bool)
/Save(...)
請將結果輸出到 TXT/JSON/PDF/DOCX。参考.aspose.com)
步骤4:将图像上传到S3
您可以从磁盘上传图像到 S3 PutObjectAsync
(您还可以上传流;两者都由 AWS SDK 支持。
// Configure S3 client (uses your AWS_PROFILE locally)
var region = RegionEndpoint.APSouth1; // change if needed
using var s3 = new AmazonS3Client(region);
// Local image you want to OCR:
string localImagePath = @"D:\samples\invoices\invoice-001.png";
string bucket = "my-ocr-demo-bucket";
string objectKey = "input/invoice-001.png";
// Upload the image to S3
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = bucket,
Key = objectKey,
FilePath = localImagePath,
ContentType = "image/png",
// Optional: enable server-side encryption
// ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256
});
Console.WriteLine($"Uploaded {objectKey} to s3://{bucket}.");
查看 AWS 的 .NET S3 上传模式的示例。AWS 文档)
步骤5:在上传图像上完成OCR
将 S3 对象直接流入内存并通过 MemoryStream
到 Aspose.OCR。
// Download S3 object and OCR in-memory (no temp files)
var get = await s3.GetObjectAsync(bucket, objectKey);
await using var s3Stream = get.ResponseStream;
using var ms = new MemoryStream();
await s3Stream.CopyToAsync(ms);
ms.Position = 0;
// Run OCR (with settings → structured result)
RecognitionResult result = ocr.RecognizeImage(ms, settings);
// Or: if you just need plain text and defaults
// string textFast = ocr.RecognizeImage(ms);
string recognizedText = result.RecognitionText;
Console.WriteLine("=== OCR TEXT ===");
Console.WriteLine(recognizedText);
是的 RecognizeImage
加载和 RecognitionResult.RecognitionText
是目前的 API 参考的一部分。参考.aspose.com)
步骤6:在 S3 中存储 OCR 结果
您可以上传平板文本,JSON,甚至由Aspose.OCR制作的PDF/DOCX。
6.a) 保存为清晰的文本
var textKey = "output/invoice-001.txt";
var textBytes = Encoding.UTF8.GetBytes(recognizedText);
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = bucket,
Key = textKey,
InputStream = new MemoryStream(textBytes),
ContentType = "text/plain"
});
Console.WriteLine($"Saved OCR text to s3://{bucket}/{textKey}");
6.b) 保存详细的 JSON
var json = result.GetJson(true); // include additional data
var jsonKey = "output/invoice-001.json";
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = bucket,
Key = jsonKey,
InputStream = new MemoryStream(Encoding.UTF8.GetBytes(json)),
ContentType = "application/json"
});
Console.WriteLine($"Saved OCR JSON to s3://{bucket}/{jsonKey}");
6.c)保存可搜索的PDF(或DOCX)并将其插入S3
// Export to PDF in-memory, then upload
using var outPdf = new MemoryStream();
result.Save(outPdf, SaveFormat.Pdf, "Arial", PdfOptimizationMode.Basic);
outPdf.Position = 0;
var pdfKey = "output/invoice-001.pdf";
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = bucket,
Key = pdfKey,
InputStream = outPdf,
ContentType = "application/pdf"
});
Console.WriteLine($"Saved OCR PDF to s3://{bucket}/{pdfKey}");
出口和储蓄方法(RecognitionResult.Save
)和格式(TXT/PDF/DOCX)在官方API参考中。
可选:终端 Program.cs
这里是一个微妙的终端版本,你可以下载到 Program.cs
(组合步骤3至6):
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Aspose.OCR;
class Program
{
static async Task Main()
{
// new License().SetLicense("Aspose.Total.lic"); // optional
string bucket = "my-ocr-demo-bucket";
string regionSystemName = "ap-south-1";
string localImagePath = @"D:\samples\invoices\invoice-001.png";
string imageKey = "input/invoice-001.png";
var ocr = new AsposeOcr();
var settings = new RecognitionSettings
{
Language = Language.Eng,
DetectAreasMode = DetectAreasMode.DOCUMENT
};
using var s3 = new AmazonS3Client(RegionEndpoint.GetBySystemName(regionSystemName));
// Upload original
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = bucket,
Key = imageKey,
FilePath = localImagePath,
ContentType = "image/png"
});
// Get image as stream
var get = await s3.GetObjectAsync(bucket, imageKey);
await using var s3Stream = get.ResponseStream;
using var ms = new MemoryStream();
await s3Stream.CopyToAsync(ms);
ms.Position = 0;
// OCR
RecognitionResult result = ocr.RecognizeImage(ms, settings);
string text = result.RecognitionText;
// Upload text
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = bucket,
Key = "output/invoice-001.txt",
InputStream = new MemoryStream(Encoding.UTF8.GetBytes(text)),
ContentType = "text/plain"
});
// Upload JSON
string json = result.GetJson(true);
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = bucket,
Key = "output/invoice-001.json",
InputStream = new MemoryStream(Encoding.UTF8.GetBytes(json)),
ContentType = "application/json"
});
// Upload PDF
using var outPdf = new MemoryStream();
result.Save(outPdf, SaveFormat.Pdf, "Arial", PdfOptimizationMode.Basic);
outPdf.Position = 0;
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = bucket,
Key = "output/invoice-001.pdf",
InputStream = outPdf,
ContentType = "application/pdf"
});
Console.WriteLine("OCR complete and results stored in S3.");
}
}
最佳实践
将 Aspose.OCR 与 AWS S3 集成,提供了许多好处,包括提高数据管理和提高规模性。
“安全”
永远不要使用硬代码的秘密
aws configure
+AWS_PROFILE
在本地;使用IAM在生产中的角色。考虑 S3 服务器侧加密(AES256 或 KMS)在结果对象上,并以最少的特权(如上所示)。AWS 文档)
性能
使用 GPU 包(
Aspose.OCR-GPU
)在 CUDA 可加速 OCR 的硬件上;同一代码,更快的执行。Aspose 文档)预处理图像为质量(Deskw、Denoise)使用
RecognitionSettings
如果需要的话,先选择正确的。DetectAreasMode
对于文件. API 选项在参考中显示。参考.aspose.com)- 可扩展*
使用 S3 Prefix 如
input/
和output/
按工作,并为可追踪提供 OCR 文件(TXT/JSON/PDF)存储。允许 S3 版本,如果您想要可审查的历史和旋转。
考虑运行此流量在容器或无服务器(例如,AWS Batch/ECS/Lambda 与 EFS)为平行 OCR 规模。
通过遵循这些指南,您可以有效地将 Aspose.OCR 与 AWS S3 集成,以便简化您的 OCR 工作流,并提高您的应用程序的整体性能。
参考
- 安东尼亚(S&D) 包和安装选项(
Aspose.OCR
,Aspose.OCR-GPU
). (Aspose 文档) AsposeOcr.RecognizeImage(...)
超重;RecognitionResult.RecognitionText
,GetJson
,Save(...)
. (参考.aspose.com)- AWS SDK for .NET: S3 创建/上传/下载示例。AWS 文档)
如果你愿意,我也可以添加一个小 Makefile
或 PowerShell 脚本运行此端到端,加上一个 CI 剪辑(GitHub 行动)将结果推到 S3 在承诺。
[4]: https://reference.aspose.com/ocr/net/aspose.ocr/recognitionresult/ “认可结果 ”