有没有办法将AWS Textract中的KeyValue对作为JSON或CSV保存到S3存储桶中?

2024-09-30 22:10:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在慢慢地学习AWS和Python上的东西,我在这里一直遵循这个例子:

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

更具体地说,表单提取位接近末尾。在

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

这是我的代码:

#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),我无法做到这一点。帮忙吗?在


Tags: andkeyfromimportclienteventjsons3