0

I'm using a Lambda python script to call Elastic Transcoder on a file that I upload. How do I delete the file after transcoding?

At the moment, my code creates the job, and then deletes the source file straight away, i.e., before the job even has a chance to run. :-)

How can I wait for Elastic Transcode to finish?

import os
import boto3
import urllib

def lambda_handler(event, context):
  transcoder = boto3.client('elastictranscoder', 'ap-southeast-2')
  pipeline_id = get_pipeline(transcoder, 'MP4 Transcode')
  base_filename = os.path.basename(event['Records'][0]['s3']['object']['key'])
  output = transcoder.create_job(
      PipelineId=pipeline_id,
      Input={
      'Key': create_aws_filename('uploads', base_filename, ''),
      'FrameRate': 'auto',
      'Resolution': 'auto',
      'AspectRatio': 'auto',
      'Interlaced': 'auto',
      'Container' : 'auto'
  })
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'])
print("deleting " + key)  
boto3.client('s3').delete_object(Bucket=bucket, Key=key)
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Evonet
  • 3,600
  • 4
  • 37
  • 83
  • 1
    Presumably the transcoded output file is written to S3. Can you have a second Lambda function triggered when the transcoded file is put, and have that function delete the source object based on the transcoded object's name? – jarmod Mar 29 '17 at 23:46
  • Yeah I think I'll need to do that. I believe Elastic Transcode can set up an SNS alert – Evonet Mar 30 '17 at 05:28

1 Answers1

2

You basically have to poll Elastic Transcoder for the job's status (every 30 seconds, for example), and wait until the job is completed. Once the job is completed, you can delete your S3 source file.

Using boto3, you can retrieve a job's status like this:

transcoder = boto3.client('elastictranscoder')
job = transcoder.read_job(Id=job_id)
status = job['Job']['Status']

Or alternatively, you can use the job_complete waiter.

spg
  • 9,309
  • 4
  • 36
  • 41
  • Thanks, end up going with the job_complete waiter, easier than launching a second lambda function (which I would need to use the job_complete waiter for anyway) – Evonet Mar 30 '17 at 05:34