简体   繁体   English

如何在 Lambda 函数中读取 S3 文件(在 python 中)

[英]How to read S3 file in Lambda function(in python)

I am trying to read a file from S3, which has the following content stored in it:我正在尝试从 S3 读取一个文件,其中存储了以下内容:

   {"empID":{"n":"7"},"name":{"s":"NewEntry"}}
   {"empID":{"n":"3"},"name":{"s":"manish"}}
   {"empID":{"n":"2"},"name":{"s":"mandeep"}}
   {"empID":{"n":"4"},"name":{"s":"Vikas"}}
   {"empID":{"n":"1"},"name":{"s":"babbar"}}

I want to iterate over each and every object and do some some processing on them.我想遍历每个对象并对它们进行一些处理。

I am taking reference from this code:我正在参考这段代码:

import json
import boto3
s3_obj =boto3.client('s3')

s3_clientobj = s3_obj.get_object(Bucket='dane-fetterman-bucket', Key='mydata.json')
s3_clientdata = s3_clientobj['Body'].read().decode('utf-8')

print("printing s3_clientdata")
print(s3_clientdata)
print(type(s3_clientdata))


s3clientlist=json.loads(s3_clientdata)
print("json loaded data")
print(s3clientlist)
print(type(s3clientlist))

but there is not any "Body" attribute in the file.但文件中没有任何“Body”属性。 Can i get some points to do the desired stuff.我可以得到一些积分来做想要的东西吗?

The issue is that the file actually contains individual JSON on each line, rather than being a complete JSON object itself.问题是该文件实际上在每一行都包含单独的 JSON,而不是一个完整的 JSON 对象本身。

Therefore, the program needs to process each line independently:因此,程序需要独立处理每一行:

import json
import boto3

s3_client = boto3.client('s3')

s3_clientobj = s3_client.get_object(Bucket='my-bucket', Key='mydata.json')

for line in s3_clientobj['Body'].iter_lines():
    object = json.loads(line)
    print(f"ID: {object['empID']['n']} Name: {object['name']['s']}")

Alternatively, you could download the whole object to disk, then just use normal for line in open('file'): syntax.或者,您可以将整个对象下载到磁盘,然后for line in open('file'):语法中使用 normal for line in open('file'):

See also: Read a file line by line from S3 using boto?另请参阅:使用 boto 从 S3 逐行读取文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从s3读取CSV文件并使用python lambda函数将内容写入RDS数据库表中? - How to read a CSV file from s3 and write the content in RDS database table using python lambda function? 如何使用 Lambda 和 Python 在 AWS s3 中读取和覆盖文件? - How to read and overwrite a file in AWS s3 using Lambda and Python? AWS Lambda 使用 python 读取 S3 存储桶中的文件 - AWS Lambda read a file in the S3 bucket using python AWS Lambda Python S3读取文件错误 - AWS Lambda Python S3 Read File Error 使用 AWS Lambda (Python 3) 读取存储在 S3 中的 Parquet 文件 - Read Parquet file stored in S3 with AWS Lambda (Python 3) 如何在从 S3 读取到 ElK 的 Lambda 函数(python 代码)中读取不同格式(异常)的行? - How to read lines with different format(exceptions) in Lambda function (python code) which reads from S3 to ElK? 如何使用 AWS Lambda Python 读取 AWS S3 存储的 word 文档(.doc 和 .docx)文件内容? - How to read AWS S3 stored word document (.doc and .docx) file content using AWS Lambda Python? 如何从 AWS Lambda 中的 s3 存储桶读取 csv 文件? - How to read csv file from s3 bucket in AWS Lambda? 如何在无服务器Lambda(Python)中下载S3文件 - how to download S3 file in Serverless Lambda (Python) AWS lambda:在本地调用可访问 s3 的 python lambda 函数 - AWS lambda: Invoking locally a python lambda function with access to s3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM