[英]AWS Lambda - Python - reading csv file in S3-uploaded packaged zip function
I have a lambda function in Python 3.6 which I compressed into a zip file to upload to S3 (total function folder size is 180MB ,that's why). 我在Python 3.6中有一个lambda函数,我将其压缩为一个zip文件以上传到S3(函数文件夹的总大小为180MB,这就是为什么)。 Inside the zip I have 1 csv file ('example.csv') which I want to read in the lambda handler function. 在zip内,我有1个csv文件(“ example.csv”),我想在lambda处理函数中读取该文件。
How to read this file? 如何读取此文件? I tried: 我试过了:
filename = 'example.csv'
filepath = os.environ['LAMBDA_TASK_ROOT'] + '/' + filename
df = pd.read_csv(filepath, dtype=str)
# Failed with OSError: Initializing from file failed
Example of the content of my lambda function folder: 我的lambda函数文件夹内容的示例:
root:
-- lambda_function.py
-- example.csv
-- bunch of library folders
Content of my csv file: 我的csv文件的内容:
id | value | something | else
-----------------------------------
0 | lol | ok | bye
1 | omg | foo | bar
2 | thx | baz | qux
What is the path to my csv file? 我的csv文件的路径是什么?
I am assuming you are using boto3 , in the documentation there is download_file
method available to download file in local. 我假设您正在使用boto3 ,在文档中有download_file
方法可用于在本地下载文件。
import boto3
import zipfile
def lambda_handler(event, context):
s3 = boto3.resource('s3')
s3.Bucket('<first_bucket>').download_file('<file_name>.zip', '/tmp/<file_name>.zip')
# put unzip code
zip_ref = zipfile.ZipFile('/tmp/<file_name>.zip', 'r')
zip_ref.extractall(directory_to_extract_to)
zip_ref.close()
#handle CSV file reading and rest of operation
After the above code, you can put your csv handling code to read and perform required operation on it. 在上面的代码之后,您可以将csv处理代码放在其中并对其执行所需的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.