获取有关上载到S3的文件的信息

时间:2017-10-17 09:19:22

标签: python-2.7 amazon-web-services amazon-s3 aws-lambda serverless-framework

我创建了一个lambda函数,只要在s3存储桶上传文件就会发送电子邮件,但现在我希望将与该文件相关的所有信息作为名称,大小,上传日期和时间,以及是否可能它来自哪里。 我在aws控制台上有所有这些infortmation,但想要在电子邮件正文中。

我正在使用无服务器框架。 v 1.22.0

这是我的代码

import json
import boto3
import botocore
import logging
import sys
import os
import traceback


from botocore.exceptions import ClientError
from pprint import pprint
from time import strftime, gmtime

email_from = '********@*****.com'
email_to = '********@*****.com'
email_subject = 'new event on s3 '
email_body = 'a new file is uploaded'


#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.INFO)

from botocore.exceptions import ClientError

def sthree(event, context):
"""Send email whenever a file is uploaded to S3"""
body = {}
status_code = 200
email_body =  str(context)

try:
    s3 = boto3.client('s3')
    ses = boto3.client('ses')
    ses.send_email(Source = email_from,
                Destination = {'ToAddresses': [email_to,],}, 
                    Message = {'Subject': {'Data': email_subject}, 'Body':{'Text' : {'Data': email_body}}}
         )
except Exception as e:
    print(traceback.format_exc())
    status_code = 500
    body["message"] = json.dumps(e)

response = {
    "statusCode": 200,
    "body": json.dumps(body)
}

return response

2 个答案:

答案 0 :(得分:4)

以下是S3在创建对象时发送的事件json结构: http://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html

您可以像这样获取文件名,大小和源IP:

for record in event['Records']:
    filename = record['s3']['object']['key'];
    filesize = record['s3']['object']['size'];
    source = record['requestParameters']['sourceIPAddress'];
    eventTime = record['eventTime'];

答案 1 :(得分:3)

<configuration>
    <components>
        <component>
            <name>hbm2java</name>
            <outputDirectory>src/main/java</outputDirectory>
            <implementation>jdbcconfiguration</implementation>
      </component>
          <component>
              <name>hbm2dao</name>
              <outputDirectory>src/main/java</outputDirectory>
              <implementation>jdbcconfiguration</implementation>
          </component>
        </components>
   <componentProperties
        <revengfile>src/main/resources/hibernate.reveng.xml</revengfile
 <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
    <jdk5>true</jdk5>
    <ejb3>false</ejb3>
   </componentProperties>
</configuration>

post找到。