Есть ли способ сохранить пары ключ-значение в AWS Textract как JSON или CSV в S3 Bucket? - PullRequest
0 голосов
/ 04 октября 2019

Я медленно изучаю вещи на AWS и Python, и я следую этому примеру здесь:

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

Точнее, бит извлечения формы в конце.

И я полагаю, если я делаю весь процесс с использованием лямбда-функции, где триггер является входом изображения 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), я не могу этого сделать. Помощь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...