【发布时间】:2020-05-04 10:58:29
【问题描述】:
TLDR;使用我的 AWS lambda doc、docx 读取存储在 S3 上的文件。
在我的本地机器上,我只使用textract.process(file_path) 来读取 doc 和 docx 文件。
因此,在 lambda 上执行相同操作的直观方法是将文件从 s3 下载到 lambda 上的本地存储 (tmp),然后像在本地计算机上一样处理 tmp 文件。
这样不划算……
有没有办法将 S3 对象的管道直接转换为 textract 之类的解析器,它只会将 doc/docx 文件转换为 string 之类的可读对象?
到目前为止,我的代码用于读取 txt 等文件。
import boto3
print('Loading function')
def lambda_handler(event, context):
try: # Read s3 file
bucket_name = "appsresults"
download_path = 'Folder1/file1.txt'
filename = download_path
s3 = boto3.resource('s3')
content_object = s3.Object(bucket_name, filename)
file_content = content_object.get()['Body'].read().decode('utf-8')
print(file_content)
except Exception as e:
print("Couldnt read the file from s3 because:\n {0}".format(e))
return event # return event
【问题讨论】:
-
not cost-effective,你的意思是 lambda 成本吗?我不确定您的每月使用情况,但您可能在免费套餐中使用 lambda。 -
by
not cost-effective我的意思是,不是只读取 s3 文件(我们必须这样做),我们还将文件保存在本地存储中(这会消耗时间)然后加载它(这也消耗时间)@Ersoy -
我明白,这是任何语言的正常行为。 Afaik,事件在 s3 上没有可用的转换/转换器管道。
标签: python amazon-s3 aws-lambda docx doc