简体   繁体   中英

How do I correct stringIO() has no attribute error in aws lambda?

I have a query that I'm trying to send to STDIN then to a csv file and copy command is failing over. It seems to fall over when I call io.StringIO()

The error I'm receiving is: Error connecting to postgres instance '_io.StringIO' object has no attribute 'getValue'

My code looks like this:

import psycopg2
from io import StringIO
import boto3
bucket = 'my_s3_bucket'
filename = 'test_data'
s3_resource = boto3.resource('s3')
conn = psycopg2.connect(host='localhost'
                        , port='5432'
                        , dbname='billing_test'
                        , user='postgres'
                        , password='password!')
cur = conn.cursor()
sql_query = "SELECT\
 * from public.customers\
  limit 1"
#cur.execute(sql_query)
#records = cur.fetchall()
#print(records)

query = '''COPY ({}) TO STDIN csv header'''.format(sql_query)

file = StringIO()
cur.copy_expert(query, file)
s3_resource.Object(bucket, f'{filename}.csv').put(Body=file.getValue())
cur.close()
conn.close()

You should read the docs, help() is your best friend in the interactive terminal:

import io
help(io.StringIO)
...
 |  getvalue(self, /)
 |      Retrieve the entire contents of the object.
 |  
...

You should use getvalue() rather than getValue()

s3_resource.Object(bucket, f'{filename}.csv').put(Body=file.getvalue())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM