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

Deleting a file after transcoding with AWS Lambda and 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)

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

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

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

或者,您可以使用 job_complete waiter.