使用AWS Lambda和Elastic Transcoder转码后删除文件

时间:2017-03-29 23:14:17

标签: python amazon-web-services amazon-s3 lambda amazon-elastic-transcoder

我正在使用Lambda python脚本在我上传的文件上调用Elastic Transcoder。如何在转码后删除文件?

目前,我的代码创建了作业,然后立即删除源文件,即在作业甚至有机会运行之前删除。 : - )

我如何等待Elastic Transcode完成?

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)

1 个答案:

答案 0 :(得分:2)

您基本上必须轮询Elastic Transcoder以获取作业的状态(例如,每30秒),并等到作业完成。作业完成后,您可以删除S3源文件。

使用boto3,您可以检索作业的状态:

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

或者,您可以使用job_complete waiter.