简体   繁体   English

AWS Lambda-Python-在S3中读取打包的zip函数中的csv文件

[英]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.

相关问题 从 AWS lambda function 中的 s3 存储桶中读取 .mdb 或 .accdb 文件并使用 python 将其转换为 excel 或 csv - Reading .mdb or .accdb file from s3 bucket in AWS lambda function and converting it into excel or csv using python 如何减小 AWS Lambda 的打包 python zip 文件的大小 - How to reduce the size of packaged python zip files for AWS Lambda python无法提取上传到aws s3存储桶的zip文件 - python unable to extract zip file uploaded to aws s3 bucket 使用AWS lambda函数使用boto3 python将S3文件从zip转换为gzip - Use AWS lambda function to convert S3 file from zip to gzip using boto3 python aws python lambda:读取 csv 文件(迭代器应返回字符串) - aws python lambda: reading csv file (iterator should return strings) 阅读上传的zip文件 - Reading uploaded zip file AWS Lambda 读取作为源代码上传的 zip 中的文件内容 - AWS Lambda read contents of file in zip uploaded as source code 使用 AWS Lambda (python) 编写 csv 文件并将其保存到 S3 中 - Write csv file and save it into S3 using AWS Lambda (python) 从 Python Azure 函数读取 Zip 文件的内容 - Reading a Zip File's Content from a Python Azure Function 使用上传到 S3 的 json 文件中的输入参数触发 AWS lambda function - Trigger AWS lambda function using input parameter from json file uploaded to S3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM