LambdaでS3上のCSVファイルの中身のレコード数を出力する関数です。
Lambda Pythonコード
import os
import time
import boto3
# main
def lambda_handler(event, context):
s3 = boto3.client('s3')
response = s3.select_object_content(
Bucket = 'バケット名',
Key = 'ファイル名',
ExpressionType = 'SQL',
Expression = 'Select count(*) from S3Object s',
InputSerialization = {
'CompressionType': 'NONE',
'CSV' : {
'FileHeaderInfo' : 'Use',
'RecordDelimiter' : '\n',
'FieldDelimiter' : ','
}
},
OutputSerialization = {
'CSV' : {
'RecordDelimiter' : '\n',
'FieldDelimiter' : ','
}
}
)
for con_event in response[ 'Payload' ]:
if 'Records' in con_event:
records = con_event[ 'Records' ][ 'Payload' ].decode( 'utf-8' )
print("Count = " + records)

