Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AWS Lambda-Python-在S3上传的打包zip函数中读取csv文件_Python_Amazon Web Services_Aws Lambda - Fatal编程技术网

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

AWS Lambda-Python-在S3上传的打包zip函数中读取csv文件,python,amazon-web-services,aws-lambda,Python,Amazon Web Services,Aws Lambda,我在Python3.6中有一个lambda函数,我将它压缩成一个zip文件以上传到S3(函数文件夹的总大小是180MB,这就是为什么)。在zip中,我有一个csv文件('example.csv'),我想在lambda处理程序函数中读取它 如何读取此文件? 我试过: “我的lambda函数”文件夹的内容示例: root: -- lambda_function.py -- example.csv -- bunch of library folders 我的csv文件的内容: id | v

我在Python3.6中有一个lambda函数,我将它压缩成一个zip文件以上传到S3(函数文件夹的总大小是180MB,这就是为什么)。在zip中,我有一个csv文件('example.csv'),我想在lambda处理程序函数中读取它

如何读取此文件? 我试过:

“我的lambda函数”文件夹的内容示例:

root:
 -- lambda_function.py
 -- example.csv
 -- bunch of library folders
我的csv文件的内容:

  id | value | something | else
-----------------------------------
  0  |  lol  |    ok     |  bye
  1  |  omg  |    foo    |  bar
  2  |  thx  |    baz    |  qux

我的csv文件的路径是什么?

我假设您正在使用,在文档中有一种方法可以在本地下载文件

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
导入boto3
进口拉链
def lambda_处理程序(事件、上下文):
s3=boto3.resource('s3')
s3.Bucket(“”)。下载_文件('.zip','/tmp/.zip')
#输入解压码
zip_ref=zipfile.zipfile('/tmp/.zip',r')
zip\u ref.extractall(目录\u到\u extract\u到)
zip_ref.close()
#处理CSV文件读取和其他操作

完成上述代码后,您可以将您的csv处理代码放入其中进行读取并执行所需操作。

您可以将其下载到/tmp/文件夹中。我该怎么做?csv文件在zip包中。请查看答案。我不认为,在本地下载之前,您可以直接解压缩s3 bucket文件。
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