Python::未从S3中的文件正确读取数据

2skhul33  于 2022-12-01  发布在  Python
关注(0)|答案(1)|浏览(98)

要求:从S3读取数据并传递到API
错误:"error": {"code": "ModelStateInvalid", "message": "The request has exceeded the maximum number of validation errors.", "target": "HttpRequest"
当我直接在代码中传递数据时,如下所示document,它工作正常

def create_doc(self,client):
        self.n_docs = int(self.n_docs)
        document = {'addresses': {'SingleLocation': {'city': 'ABC', 
                                'country': 'US', 
                                'line1': 'Main', 
                                'postalCode': '00000', 
                                'region': 'CA'
                                    }
                                }, 
                            'commit': False, 
                            }
       response = client.cr_transc(document)            
       jsn = response.json()

但是,当尝试将文件中的数据保存在s3中并从s3中读取数据时,会引发错误

def create_doc(self,client):
        self.n_docs = int(self.n_docs)
        document = data_from_s3()
        response = client.cr_transc(document)            
        jsn = response.json()

def data_from_s3(self):
     s3 = S3Hook()
     data = s3.read_key(bucket_name = self.bucket_name, key = self.data_key)
     return data

下面的链接是针对气流中的read_key方法
如果您有任何问题,请与我们联系。如果您有问题,请与我们联系。

92dk7w1h

92dk7w1h1#

检查源代码:

def read_key(self, key, bucket_name=None):
    """
    Reads a key from S3

    :param key: S3 key that will point to the file
    :type key: str
    :param bucket_name: Name of the bucket in which the file is stored
    :type bucket_name: str
    """

    obj = self.get_key(key, bucket_name)
    return obj.get()['Body'].read().decode('utf-8')

这将返回一个str。您可能需要使用json模块来转换它:

import json

def create_doc(self,client):
    self.n_docs = int(self.n_docs)
    document = json.loads(data_from_s3()) # <----- convert here
    response = client.cr_transc(document)            
    jsn = response.json()

def data_from_s3(self):
    s3 = S3Hook()
    data = s3.read_key(bucket_name = self.bucket_name, key = self.data_key)
    return data

相关问题