0

我在 AWS 和 Python 上慢慢地学习东西,我一直在关注这个例子:

https://aws.amazon.com/blogs/machine-learning/automatically-extract-text-and-structured-data-from-documents-with-amazon-textract/

更具体地说,表格提取位接近尾声。

而且我想如果我使用 Lambda 函数执行整个过程,其中触发器是 S3 图像输入,有没有办法将 Analyse_Document 函数产生的键值对保存为 json 或 CSV 相同S3 存储桶?

这是我的代码:

#Loading AWS CLI and Packages
import json
import boto3
import os
import urllib.parse

print('Loading function')

#S3 client
s3 = boto3.client('s3')

# Amazon Textract client
textract = boto3.client('textract')

def getTextractData(bucketName, documentKey):
    print('Loading getTextractData')
    # Call Amazon Textract
    response = textract.analyze_document(
        Document={
            'S3Object': {
                'Bucket': bucketName,
                'Name': documentKey
            }
        },
        FeatureTypes=["FORMS"])

    forms_json = []

    for page in doc.pages:
        for field in page.form.fields:
            print("Key: {}, Value: {}".format(field.key, field.value))

def lambda_handler(event, context):
    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
    try:
        detectedText = getTextractData(bucket, key)
        writeTextractToS3File(detectedText, bucket, key)

        return 'Processed'

    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

我之前能够生成一个文本文件,但现在我已经更改了代码以获取 CSV 或 JSON(对于 DynamoDB),我无法做到这一点。帮助?

4

0 回答 0