使用AWS Lambda和Elastic Transcoder转码后删除文件 [英] Deleting a file after transcoding with AWS Lambda and Elastic Transcoder

查看:145
本文介绍了使用AWS Lambda和Elastic Transcoder转码后删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



此刻,我的代码创建了作业,然后立即删除了源文件,即在作业之前跑步的机会。 :-)



如何等待Elastic Transcode完成?

  import os 
import boto3
import urllib

def lambda_handler(event,context):
转码器= boto3.client('elastictranscoder','ap-southeast -2')
pipe_id = get_pipeline(transcoder,'MP4 Transcode')
base_filename = os.path.basename(event ['Records'] [0] ['s3'] ['object'] ['key'])
输出= 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' ] ['状态']

或者,您也可以使用 job_complete 服务员。

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)

解决方案

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.

这篇关于使用AWS Lambda和Elastic Transcoder转码后删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆