S3にアップロードされたファイルがmov形式だった場合、mp4形式にエンコードするという処理をlambdaで実装しています。
mp4形式にエンコードされた後、エンコード前のmovファイルは自動で削除される処理を加えたいのですが、どのようにすればよいか分かりません。
分かる方いらっしゃいましたら、ご教授願います。
以下現状のlambdaのコードです。
import boto3 from botocore.client import ClientError import json import urllib REGION_NAME = 'ap-northeast-1' TRANSCODER_ROLE_NAME = 'lambda_auto_transcoder_role' PIPELINE_NAME = 'testPipeline' OUT_BUCKET_NAME = 'baket-in' COMPLETE_TOPIC_NAME = 'test-complete' print('Loading function') s3 = boto3.resource('s3') iam = boto3.resource('iam') sns = boto3.resource('sns', REGION_NAME) transcoder = boto3.client('elastictranscoder', REGION_NAME) def lambda_handler(event, context): #print("Received event: " + json.dumps(event, indent=2)) # Get ARN complete_topic_arn = sns.create_topic(Name=COMPLETE_TOPIC_NAME).arn transcoder_role_arn = iam.Role(TRANSCODER_ROLE_NAME).arn # Get the object from the event bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8') print("bucket={}, key={}".format(bucket, key)) try: obj = s3.Object(bucket, key) except Exception as e: print(e) print("Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.".format(key, bucket)) # Publish a message sns.Topic(complete_topic_arn).publish( Subject="Error!", Message="Failed to get object from S3. bucket={}, key={}, {}".format(bucket, key, e), ) raise e # Delete inactive pipelines pipeline_ids = [pipeline['Id'] for pipeline in transcoder.list_pipelines()['Pipelines'] if pipeline['Name'] == PIPELINE_NAME] for pipeline_id in pipeline_ids: try: response = transcoder.delete_pipeline(Id=pipeline_id) print("Delete a transcoder pipeline. pipeline_id={}".format(pipeline_id)) print("response={}".format(response)) except Exception as e: # Raise nothing print("Failed to delete a transcoder pipeline. pipeline_id={}".format(pipeline_id)) print(e) # Create a pipeline try: response = transcoder.create_pipeline( Name=PIPELINE_NAME, InputBucket=bucket, OutputBucket=OUT_BUCKET_NAME, Role=transcoder_role_arn, Notifications={ 'Progressing': '', 'Completed': complete_topic_arn, 'Warning': '', 'Error': '' }, ) pipeline_id = response['Pipeline']['Id'] print("Create a transcoder pipeline. pipeline_id={}".format(pipeline_id)) print("response={}".format(response)) except Exception as e: print("Failed to create a transcoder pipeline.") print(e) # Publish a message sns.Topic(complete_topic_arn).publish( Subject="Error!", Message="Failed to create a transcoder pipeline. bucket={}, key={}, {}".format(bucket, key, e), ) raise e # Create a job try: job = transcoder.create_job( PipelineId=pipeline_id, Input={ 'Key': key, 'FrameRate': 'auto', 'Resolution': 'auto', 'AspectRatio': 'auto', 'Interlaced': 'auto', 'Container': 'auto', }, Outputs=[ { 'Key': 'output/{}'.format('.'.join(key.split('.')[:-1])) + '.mp4', 'PresetId': '1351620000001-000061' }, ], ) job_id = job['Job']['Id'] print("Create a transcoder job. job_id={}".format(job_id)) print("job={}".format(job)) except Exception as e: print("Failed to create a transcoder job. pipeline_id={}".format(pipeline_id)) print(e) # Publish a message sns.Topic(complete_topic_arn).publish( Subject="Error!", Message="Failed to create transcoder job. pipeline_id={}, {}".format(pipeline_id, e), ) raise e return "Success"
以下を追加することで画像は削除出来るようになったのですが、そうするとエンコードのファイルが生成される前にエンコード前のファイルが削除されてしまうので実行タイミングをエンコード後とする必要があります。
何か良い方法はありましたら教えていただきたく存じます。
#エンコード前のファイルを削除 try: response = s3.delete_object( Bucket=OUT_BUCKET_NAME, Key=key, #MFA='string', #VersionId='string', #RequestPayer='requester' ) print(key) print("is deleted") except Exception as e: print(key) print("not deleted")
よろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。