gpt4 book ai didi

python - 使用lambda python将文件上传到s3后用表单数据更新mysql

转载 作者:行者123 更新时间:2023-11-30 21:54:47 24 4
gpt4 key购买 nike

我正在尝试使用表单将文件上传到 S3。该表单还有一些其他数据,虽然文件确实上传到存储桶,但我只是不明白如何获取其余数据以在 Lambda 函数中使用它来更新 RDS 实例中的 mysql 数据库。

这是我的表格(改编自 http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html )

    <form name='form10' action="http://<?= $my_bucket ?>.s3.amazonaws.com/" method="post" enctype="multipart/form-data" onsubmit="return v.exec()">
<input type="hidden" name="key" value="videos/${filename}" />
<input type="hidden" name="acl" value="public-read" />
<input type="hidden" name="X-Amz-Credential" value="<?= $access_key; ?>/<?= $short_date; ?>/<?= $region; ?>/s3/aws4_request" />
<input type="hidden" name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
<input type="hidden" name="X-Amz-Date" value="<?=$iso_date ; ?>" />
<input type="hidden" name="Policy" value="<?=base64_encode($policy); ?>" />
<input type="hidden" name="X-Amz-Signature" value="<?=$signature ?>" />
<input type="hidden" name="success_action_redirect" value="<?= $success_redirect ?>" />

<table summary="Playlist de Campa&ntilde;a" align=center border="1">
<tr><td colspan="2" align="center">Agregar Videos a Playlist</td></tr>
<tr><td>Selecciona tu Archivo:</td><td><input type="file" name="file" /></td></tr>
<tr><td>Descripci&oacute;n del Video</td><td><input type="text" name="desc_video" value="" size="50"/></td></tr>
<tr><td>Fecha Inicio:
<input type=text name=start_date value="<?PHP echo $fecha_inicio; ?>"><a href="javascript:show_calendar('form10.start_date');" ><img src="images/show-calendar.gif" width="24" height="22" border="0" alt=""></a>
</td><td>Fecha Terminaci&oacute;n
<input type=text name=end_date value="<?PHP echo $fecha_termina;?>"><a href="javascript:show_calendar('form10.end_date');" ><img src="images/show-calendar.gif" width="24" height="22" border="0" alt=""></a>
</td></tr>
<tr><td>Hora Inicio:
<select name="hora_inicio">
<?PHP
for ($hr = 0; $hr < 24; $hr++){
echo"<option value=$hr>$hr</option>";
}
?>
</select>:
<select name="mins_inicio">
<?PHP
for ($hr = 0; $hr < 60; $hr+=5){
echo"<option value=$hr>$hr</option>";
}
?>
<option value="59">59</option>
</select></td>
<td>Hora Terminaci&oacute;n:
<select name="hora_final">
<?PHP
for ($hr = 0; $hr < 24; $hr++){
if($hr<>23){
echo"<option value=$hr>$hr</option>";
} else {
echo"<option value=$hr selected>$hr</option>";
}

}
?>
</select>:
<select name="mins_final">
<?PHP
for ($hr = 0; $hr < 60; $hr+=5){
echo"<option value=$hr>$hr</option>";
}
?>
<option value="59" selected>59</option>
</select></td>
</tr>
<tr><td colspan="2" align="center"><input type="submit" value="Continuar" /></td></tr>
</table>
<input type="hidden" name="step" value="sube" />
<input type="hidden" name="tipo" value="<?PHP echo $tipo; ?>" />
<input type="hidden" name="id_campana" value="<?PHP echo $id_campana; ?>" />
<input type="hidden" name="fecha_inicio" value="<?PHP echo $fecha_inicio; ?>" />
<input type="hidden" name="fecha_termina" value="<?PHP echo $fecha_termina; ?>" />

</form>

如您所见,我有一堆变量需要保存到我的数据库中,但我只是不知道如何在 Lambda 函数中调用它们。 Lambda 函数可以写入数据库。我有一个能够写入数据库的硬编码 sql 语句,当 S3 存储桶接收到文件时它会自行触发。这是我正在使用的 lambda 函数,(改编自:https://www.isc.upenn.edu/accessing-mysql-databases-aws-python-lambda-function)

import sys
import logging
import rds_config
import pymysql
#rds settings
rds_host = "xxxxxxxxxxxxxxxxxxxx.rds.amazonaws.com"
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name
logger = logging.getLogger()
logger.setLevel(logging.INFO)

try:
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
sys.exit()
logger.info("SUCCESS: Connection to RDS mysql instance succeeded")
def lambda_handler(event, context):
try:
cur = conn.cursor()
cur.execute("INSERT INTO archivos (id_cliente,id_campana,tipo_asset,id_tipo_asset,file_name,original_name,descripcion,file_type,date_begin,date_end,time_begin,time_end,MD5) VALUES (5,11,'Marca',3,'test.png','test.png','este es el video','png','2017-08-01','2017-08-30','00:00:00','23:59:00','90175908345798dhf')")
conn.commit()
except MySQLError as e:
logger.error(e)
return None

关于如何实现此目标的任何想法?我敢肯定这真的很愚蠢,但我已经尝试了好几天了,但我似乎无法理解它。

最佳答案

将所有内容成功上传到 S3 后,您可以创建从 S3 到 Lambda 的触发器。该事件包含有关上传的 S3 对象的所有信息。

你也可以读取S3对象,提取你想要的字段存储到RDS。

以下是为 Lambda 创建 S3 触发器的文档。

http://docs.aws.amazon.com/lambda/latest/dg/with-s3.html

您还可以根据新帖子、更新或删除自定义触发器。

希望这对您有所帮助。

关于python - 使用lambda python将文件上传到s3后用表单数据更新mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45665388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com