在AWS S3 Bucket中从多个PDF中搜索2个字符串,该Bucket具有子目录,而不下载本地机器中的子目录



我想在AWS S3存储桶中的多个PDF中搜索两个单词。然而,我不想在本地机器上下载这些文档,相反,如果搜索部分可以通过URL直接在那些pdf上运行的话。需要注意的是,这些PDF位于一个bucket中的多个子目录中(如年份文件夹、月份文件夹和日期(。

Amazon S3没有"搜索"功能。它是一个";简单存储服务";。

您需要将这些文档下载到某种形式的计算平台(如EC2、Lambda或您自己的计算机(并执行搜索,您可以使用类似Amazon OpenSearch service的服务对文档进行预索引,然后将查询发送到搜索服务。

运行PDF的直接扫描以搜索S3存储桶中的文本是困难的:

  • 一些PDF包含嵌入图像中的文本(它们以文本形式不可读(
  • 如果你想下载PDF而不保存它,请考虑使用内存优化的机器,不要将文件存储在虚拟机的硬盘驱动器中,也不要使用内存流
  • 为了绕过图像中的文本,需要使用OCR逻辑,这也很难执行。您将非常希望使用AWS Textract或Google Vision进行OCR。如果合规性和安全性存在问题,您可以使用Tesseract
  • 如果你有一个可靠的OCR解决方案,我建议你在上传事件发生后运行文本提取作业,这将为你节省大量的钱来支付你将使用的任何OCR服务,它还将使你的组织能够在更方便搜索的服务中以文本格式缓存pdf的内容,如AWS OpenSearch

这里有一个教程,它使用Tika(用于PDF OCR(和OpenSearch(用于搜索引擎(在S3存储桶中搜索PDF文件的内容:

import boto3
from tika import parser
from opensearchpy import OpenSearch
from config import *
import sys

# opensearch object
os = OpenSearch(opensearch_uri)
s3_file_name="prescription.pdf"
bucket_name="mixpeek-demo"

def download_file():
"""Download the file
:param str s3_file_name: name of s3 file
:param str bucket_name: bucket name of where the s3 file is stored
"""
# s3 boto3 client instantiation
s3_client = boto3.client(
's3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region_name
)
# open in memory
with open(s3_file_name, 'wb') as file:
s3_client.download_fileobj(
bucket_name,
s3_file_name,
file
)
print("file downloaded")
# parse the file
parsed_pdf_content = parser.from_file(s3_file_name)['content']
print("file contents extracted")
# insert parsed pdf content into elasticsearch
insert_into_search_engine(s3_file_name, parsed_pdf_content)
print("file contents inserted into search engine")

def insert_into_search_engine(s3_file_name, parsed_pdf_content):
"""Download the file
:param str s3_file_name: name of s3 file
:param str parsed_pdf_content: extracted contents of PDF file
"""
doc = {
"filename": s3_file_name,
"parsed_pdf_content": parsed_pdf_content
}
# insert
resp = os.index(
index = index_name,
body = doc,
id = 1,
refresh = True
)
print('nAdding document:')
print(resp)

def create_index():
"""Create the index
"""
index_body = {
'settings': {
'index': {
'number_of_shards': 1
}
}
}
response = os.indices.create(index_name, body=index_body)
print('nCreating index:')
print(response)

if __name__ == '__main__':
globals()[sys.argv[1]]()

完整教程:https://medium.com/@mixepeek/search-text-from-pdf-files-stored-in-an-s3-buket-2f10947eebd3

对应的github回购:https://github.com/mixpeek/pdf-search-s3

最新更新