0

I have written a function in Python to analyse documents and retrieve key-value pairs from forms in these documents. Below is just the handler bit of it.

def handler(event, context):
  bucket = event['Records'][0]['s3']['bucket']['name']
  key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

  try:
      document = TTFunction(bucket,key)
      KVpairs=[]
      for page in document.pages:
        KVpairs= output(page)

When I print the output of this function, it is in JSON. Is there a way to save this output as a JSON or a CSV in the same bucket?

balderman
  • 22,927
  • 7
  • 34
  • 52
Greation
  • 73
  • 1
  • 2
  • 12
  • 1
    Yes. See boto3 lib about writing data to bucket. https://boto3.amazonaws.com/v1/documentation/api/latest/index.html?id=docs_gateway – balderman Oct 13 '19 at 07:52
  • Alright, but I have tried both put_object and upload_file and I couldn't get the function to save. Do I need to save the file first in a temp directory or can I do that directly? – Greation Oct 13 '19 at 08:42
  • No need to save anything – balderman Oct 13 '19 at 08:55
  • Okay, I'd really appreciate any suggestions on this. I am really new to all of this. I was able to do that in my local drive but not on S3. – Greation Oct 13 '19 at 09:06
  • See https://stackoverflow.com/questions/40336918/how-to-write-a-file-or-data-to-an-s3-object-using-boto3 – balderman Oct 13 '19 at 10:09
  • Possible duplicate of [How to write a file or data to an S3 object using boto3](https://stackoverflow.com/questions/40336918/how-to-write-a-file-or-data-to-an-s3-object-using-boto3) – Lamanus Oct 13 '19 at 10:30
  • This will help you understand boto3 https://realpython.com/python-boto3-aws-s3/ – amittn Oct 13 '19 at 11:39

2 Answers2

0
  • This is just a sample codeaws docs
  • boto3 docs for put_object note the body can be bytes or file.
  • You lambda must have WRITE permissions on a bucket to add an object to it.
    # Put the object
    s3 = boto3.client('s3')
    try:
        s3.put_object(Bucket=dest_bucket_name, Key=dest_object_name, Body=object_data)
    except ClientError as e:
        logging.error(e)
        return False
amittn
  • 2,249
  • 1
  • 12
  • 19
0

I actually found the code for that. One of the problems I had was converting the body to Bytes, but I found a way. I will post the code soon.

Greation
  • 73
  • 1
  • 2
  • 12