我正在寻找编辑我的 lambda,以便在其作业状态为“完成”时删除转录作业。我有以下代码:
import json
import time
import boto3
from urllib.request import urlopen
def lambda_handler(event, context):
transcribe = boto3.client("transcribe")
s3 = boto3.client("s3")
if event:
file_obj = event["Records"][0]
bucket_name = str(file_obj["s3"]["bucket"]["name"])
file_name = str(file_obj["s3"]["object"]["key"])
s3_uri = create_uri(bucket_name, file_name)
file_type = file_name.split("2019.")[1]
job_name = file_name
transcribe.start_transcription_job(TranscriptionJobName=job_name,
Media ={"MediaFileUri": s3_uri},
MediaFormat = file_type,
LanguageCode = "en-US",
Settings={
"VocabularyName": "Custom_Vocabulary_by_Brand_Other_Brands",
"ShowSpeakerLabels": True,
"MaxSpeakerLabels": 4
})
while True:
status = transcribe.get_transcription_job(TranscriptionJobName=job_name)
if status["TranscriptionJob"]["TranscriptionJobStatus"] in ["FAILED"]:
break
print("It's in progress")
while True:
status = transcribe.get_transcription_job(TranscriptionJobName=job_name)
if status["TranscriptionJob"]["TranscriptionJobStatus"] in ["COMPLETED"]:
transcribe.delete_transcription_job(TranscriptionJobName=job_name
)
time.sleep(5)
load_url = urlopen(status["TranscriptionJob"]["Transcript"]["TranscriptFileUri"])
load_json = json.dumps(json.load(load_url))
s3.put_object(Bucket = bucket_name, Key = "transcribeFile/{}.json".format(job_name), Body=load_json)
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
def create_uri(bucket_name, file_name):
return "s3://"+bucket_name+"/"+file_name
处理这项工作的部分是:
while True:
status = transcribe.get_transcription_job(TranscriptionJobName=job_name)
if status["TranscriptionJob"]["TranscriptionJobStatus"] in ["FAILED"]:
break
print("It's in progress")
while True:
status = transcribe.get_transcription_job(TranscriptionJobName=job_name)
if status["TranscriptionJob"]["TranscriptionJobStatus"] in ["COMPLETED"]:
transcribe.delete_transcription_job(TranscriptionJobName=job_name
)
如果作业正在进行中,它会说“正在进行中”,但当它显示“已完成”时,它会删除。
任何想法为什么我当前的代码不起作用?它会完成转录作业,但不会将其删除。