質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

2114閲覧

Amazon Elastic Transcoderでエンコードした後、input側のファイルを削除する方法

kuriya

総合スコア35

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2017/05/18 05:47

編集2017/05/19 09:46

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")

よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

Offload S3のプラグインのソースを書き換える。
amazon-s3-and-cloudfront-pro/classes/amazon-s3-and-cloudfront.php
のadd_post_metaでDBに保存しているので、その手前で入るURLをよしなにreplaceしすることで対応出来ました。

投稿2017/05/22 05:54

kuriya

総合スコア35

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問