如何更正 stringIO() 在 aws lambda 中没有属性错误?

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

我有一个查询要发送到 STDIN 然后发送到 csv 文件,复制命令失败。我叫io.StringIO()

的时候好像摔倒了

我收到的错误是:Error connecting to postgres instance '_io.StringIO' object has no attribute 'getValue'

我的代码如下所示:

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()

你应该阅读文档,help() 是你在交互式终端中最好的朋友:

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

您应该使用 getvalue() 而不是 getValue()

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