image-uploaded
and rekognition-ouput
<lambda-function-name>
<event-name>
image-uploaded/
.jpg
import json
import boto3
s3_client = boto3.client('s3')
rekognition_client = boto3.client('rekognition')
def lambda_handler(event, context):
# Get the bucket name and object key from the event
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
# Call Rekognition to detect labels
response = rekognition_client.detect_labels(
Image={
'S3Object': {
'Bucket': bucket,
'Name': key
}
},
MaxLabels=10
)
# Save the response to the output folder
output_key = 'rekognition-output/' + key.split('/')[-1] + '.json'
s3_client.put_object(
Bucket=bucket,
Key=output_key,
Body=json.dumps(response, indent=4)
)
return {
'statusCode': 200,
'body': json.dumps('Image processed and labels detected successfully')
}
image-uploaded
foldercomprehend-text-input
and comprehend-analysis-output
<lambda-function-name>
import json
import boto3
# Initialize S3 and Comprehend clients
s3_client = boto3.client('s3')
comprehend_client = boto3.client('comprehend')
def lambda_handler(event, context):
# Extract bucket and key (file name) from the S3 event
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
# Get the text content from the uploaded file in S3
response = s3_client.get_object(Bucket=bucket, Key=key)
text = response['Body'].read().decode('utf-8')
# Perform sentiment analysis using Comprehend
sentiment_response = comprehend_client.detect_sentiment(
Text=text,
LanguageCode='en' # Set the language of the text
)
# Save the sentiment analysis result to the output bucket
output_key = 'comprehend-analysis-output/' + key.split('/')[-1].replace('.txt', '') + '-sentiment.json'
s3_client.put_object(
Bucket=bucket,
Key=output_key,
Body=json.dumps(sentiment_response, indent=4)
)
return {
'statusCode': 200,
'body': json.dumps('Sentiment analysis completed successfully.')
}
text-upload-event
comprehend-text-input/
<lambda-function-name>
##Β Test the Lambda Function
comprehend-text-input
folderpolly-text-input
and polly-audio-output
<lamba-func-name>
import json
import boto3
# Initialize S3 and Polly clients
s3_client = boto3.client('s3')
polly_client = boto3.client('polly')
def lambda_handler(event, context):
# Extract bucket and key (file name) from the S3 event
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
# Get the text content from the uploaded file in S3
response = s3_client.get_object(Bucket=bucket, Key=key)
text = response['Body'].read().decode('utf-8')
# Convert the text to speech using Polly
polly_response = polly_client.synthesize_speech(
Engine='standard',
Text=text,
OutputFormat='mp3',
VoiceId='Joanna' # You can choose a different voice from Polly
)
# Save the audio file to a different S3 bucket
output_key = 'polly-audio-output/' + key.split('/')[-1].replace('.txt', '') + '.mp3'
s3_client.put_object(
Bucket=bucket,
Key=output_key,
Body=polly_response['AudioStream'].read()
)
return {
'statusCode': 200,
'body': json.dumps('Text to speech conversion completed successfully.')
}
text-upload-event
polly-text-input/
polly-text-input/
folderpolly-audio-output/
folder