我对AWS和lambda不熟悉,因此,如果这是一个愚蠢的问题,我深表歉意。我想做的是将电子表格加载到s3存储桶中,基于该上传触发lambda,让lambda将csv加载到熊猫中并对其进行处理,然后将数据帧写回到csv中并放入第二个s3中桶。
我已经阅读了很多有关压缩python脚本以及所有库和依赖项并将其上传的知识,这就是一个单独的问题。我还想出了如何在将文件上传到S3存储桶时触发lambda,以及如何自动将该文件复制到第二个S3存储桶。
我无法找到任何信息的部分是中间部分,将文件加载到pandas中,并在lambda函数内部的所有pandas中操作文件。
第一个问题:那样的事情可能吗?
第二个问题:如何从s3存储桶中“抓取”文件并将其加载到熊猫中?会是这样吗?
import pandas as pd
import boto3
import json
s3 = boto3.resource('s3')
def handler(event, context):
dest_bucket = s3.Bucket('my-destination-bucket')
df = pd.read_csv(event['Records'][0]['s3']['object']['key'])
# stuff to do with dataframe goes here
s3.Object(dest_bucket.name, <code for file key>).copy_from(CopySource = df)
?我真的不知道那是不是还很接近,并且在黑暗中是完整的镜头。我们将不胜感激任何帮助,因为我显然不适合我!
参考方案
此代码在PUTS上触发Lambda函数,然后对其进行GETS,然后将其PUTS到另一个存储桶中:
from __future__ import print_function
import os
import time
import json
import boto3
s3 = boto3.client('s3')
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = quote(event['Records'][0]['s3']['object']['key'].encode('utf8'))
try:
response = s3.get_object(Bucket=bucket, Key=key)
s3_upload_article(response, bucket, end_path)
return response['ContentType']
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e
def s3_upload_article(html, bucket, end_path):
s3.put_object(Body=html, Bucket=bucket, Key=end_path, ContentType='text/html', ACL='public-read')
我从编写的更复杂的Lambda脚本中拆分了此代码,但是,我希望它显示您需要做的一些事情。对象的PUTS仅触发密码。触发事件后发生的任何其他操作取决于您是否要编写脚本。
bucket = event['Records'][0]['s3']['bucket']['name']
key = quote(event['Records'][0]['s3']['object']['key'].encode('utf8'))
前几行中的存储桶和密钥是触发事件的对象的存储桶和密钥。其他一切都取决于您。
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
如何用'-'解析字符串到节点js本地脚本? - python我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…
将字符串分配给numpy.zeros数组[重复] - pythonThis question already has answers here: Weird behaviour initializing a numpy array of string data (4个答案) …
Python:传递记录器是个好主意吗? - python我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…
Python-Excel导出 - python我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…