Send a trigger to Lambda function when I upload to an S3 Bucket folder

0

how do I send a trigger to the Lambda function when uploading to an S3 Bucket folder, since I already went to Properties and made a s3 notification to activate the lambda when uploading but it still doesn't work. My code is

import json from urllib.parse import unquote_plus import boto3 import uuid

transcribe = boto3.client("transcribe")

def lambda_handler(event, context): file_obj = event["Records"][0] domenicoteste1 = str(file_obj["s3"]["bucket"]["name"])

for upload in domenicoteste1:
    # TODO: write code... event:
    
    file_obj = event["Records"][0]
    domenicoteste1 = str(file_obj["s3"]["bucket"]["name"])
    teste = unquote_plus(str(file_obj["s3"]["object"]["key"]))
    s3_uri = create_uri(domenicoteste1, teste)
    job_name = context.aws_request_id

    transcribe.start_transcription_job(
        TranscriptionJobName= job_name,
        Media={"MediaFileUri": "s3://domenicoteste1/teste.mp4"},
        MediaFormat="mp4" or "mp3",
        LanguageCode="pt-BR",
        OutputBucketName="outputeteste1",
        Settings={
            # 'VocabularyName': 'string',
           "ShowSpeakerLabels": True,
            "MaxSpeakerLabels": 2,
            "ChannelIdentification": False,
        },
    )

return {"statusCode": 200, "body": json.dumps("Transcription job is created!")}

def create_uri(domenicoteste1, teste): return "s3://domenicoteste1/teste.mp4"

4 Answers
0

Have you made sure that the lambda resource policy allows the S3 to invoke the lambda. Please take a look at this document - https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html

profile picture
EXPERT
answered 2 months ago
  • Good morning, Indranil Banerjee , thank you very much for answering, but I took a look at the documentation and saw the Event Bridge still doesn't work I put it there in the console as s3 Creat trigger, so that every time I create the event it works, but when i upload it does not go. Is there a s3-specific custom Json function that I can put so that every s3 upload runs Lambda?

  • Hi Domenico - My link does not talk about Eventbridge at all. It is a simple S3 event source for lambda. There is a tutorial as well that shows you how to configure this - https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html

0

Hi,

Have a look at the following blog post https://aws.amazon.com/blogs/compute/using-dynamic-amazon-s3-event-handling-with-amazon-eventbridge/ that describes how to trigger a lambda function when a object is uploaded to a S3 bucket.

The following github repository https://github.com/geoffstratton/aws-lambda-transcribe-audio has a lambda function for audio transcription.

Nuno_Q
answered 2 months ago
  • Good morning, Nuno_Q, thank you very much for answering, but I took a look at the documentation and saw the Event Bridge still doesn't work I put it there in the console as s3 Creat trigger, so that every time I create the event it works, but when I upload it, it won't, in respect to the code, when I run it, it's giving an empty s3 bucket, and I have objects there, look at the new code: import json from urllib.parse import unquote_plus import boto3 import uuid import os

    def lambda_handler(event, context):

    record = event['Records'][0]
    
    domenicoteste1 = record['s3']['bucket']['name']
    s3object = record['s3']['object']['key']
    
    file_obj = event["Records"][0]
    object = unquote_plus(str(file_obj["s3"]["object"]["key"]))
    
    s3Path = "s3://" + domenicoteste1 + "/" + s3object
    jobName = domenicoteste1 + '-' + str(uuid.uuid4())
    
    client = boto3.client('transcribe')
    
    response = client.start_transcription_job(
        TranscriptionJobName=jobName,
        LanguageCode='pt-BR',
        MediaFormat='mp3',
        Media={
            'MediaFileUri': s3Path
        },
        OutputBucketName= "outputeteste1"
    )
    
    return {
        'TranscriptionJobName': response['TranscriptionJob']['TranscriptionJobName']
    }
    

    o ERRO é: "errorMessage": "An error occurred (BadRequestException) when calling the StartTranscriptionJob operation: The URI that you provided doesn't point to an S3 object. Make sure that the object exists and try your re

0

Hi, Did you try adding a trigger from Lambda function configuration console?

Enter image description here

answered 2 months ago
0

Hi, yes i did it

Enter image description here

profile picture
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions