AWS Lambda(python 3.6)将图像从S3 bucket添加到html主体

2024-09-30 20:28:24 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在使用我的aws lambda通过ses发送电子邮件。我的S3桶正在触发我的lambda。我需要将来自其他S3 bucket的png图像添加到我的html正文中

我的代码:


import json
import boto3
import csv
from datetime import datetime
 
def lambda_handler(event, context):
    for i in event['Records']:
        bucket_name = i['s3']['bucket']['name']
        object = i['s3']['object']['key']
        s3=boto3.client('s3')
        response=s3.get_object(Bucket=bucket_name,Key=object)
        csv_contents = response['Body'].read()
        csv_contents_decoded = csv_contents.decode('utf-8')
        reader=csv.reader(csv_contents_decoded.splitlines())
        print('type csv_contents_decoded:',type(csv_contents_decoded))
        print('type reader:',type(reader))
        print('csv_contents_decoded:',csv_contents_decoded)
        print('reader:',reader)
        
        
        currentYear = datetime.now().year
        curryear = int(str(currentYear)[-2:])
        months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        current_month = datetime.now().month -1
        current_mnth = [months[(current_month)]]
        curr_mnth = ''.join(current_mnth)
        previous_mnth = [months[(current_month)-1]]
        prev_mnth = ''.join(previous_mnth)
        
        to_list = []
        cc_list = []
        for row in reader:
            to_list.append(row[1]+'@example.com')
            cc_list.append(row[2]+'@example.com')
        
            
            
        
        
    client = boto3.client('ses')
    subject = f'''[Update reminder] for {prev_mnth}'{curryear}'''
    table = """
    <html>
    <head>
    <style>
    table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
    }
    th, td {
    padding: 15px;}
    
    </style>
    </head>
    <body>
 
    <table style="width:100%">
    <tr>
    <td>A</td>
    <td>B</td>
    <td>C</td>
    <td>D</td>
    </tr>
    <tr>
    <td>E</td>
    <td>text...</td>
    </tr>
    <tr>
    ...
    ...
    </tr>
    </table>
    </body>
    </html>
    """
    
    body = f'''
         <br>
         text...,<br>
        
         {table}
         <br>
         Thank you...
 
    '''
 
    message = {'Subject': {'Data': subject}, 'Body': {'Html': {'Data': body}}}
    response = client.send_email(Source = 'example@example.com', Destination = {'ToAddresses': to_list},Message=message)    
        
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }



我正在尝试使用html图像标记: <;img src=“S3 bucket url”,style=“最大宽度:180px;最大高度:300px;高度:自动;宽度:自动;页边距顶部:8px;”/>

我没有得到任何输出


Tags: csvimports3buckethtmlcontentstablebody