【问题标题】:AWS SDK Media Convert error handling in laravellaravel 中的 AWS 开发工具包媒体转换错误处理
【发布时间】:2021-08-16 13:25:39
【问题描述】:

我有一个作业 ID 列表来检查它们的状态。所以,我只是遍历所有的 Job ID 来获取它们在 Media Convert 上的状态。

function get_aws_job_id_status($job_id)
{
    $result = [];
    $client = \App::make('aws')->createClient('MediaConvert', [

        // 'profile' => 'default',
        // 'version' => '2017-08-29',
        'region'  => 'region',
        'endpoint' => "endpoint"
    ]);
    try {
        $result = $client->getJob([
            'Id' => $job_id,
        ]);
        return $result;
    } catch (AwsException $e) {
        return $result;
    }
}

我在循环中使用上述函数来获取状态。 参考AWS DocsStackoverflow,但是,当我没有找到给定作业 ID 的记录时,它返回“NotFoundException”错误,该错误不会进入 catch 块并打破循环。有什么方法可以处理该异常,以便我可以继续循环?

【问题讨论】:

    标签: php aws-sdk aws-php-sdk aws-media-convert


    【解决方案1】:

    我相信您需要致电 Aws\MediaConvert\Exception\MediaConvertException 并捕获 MediaConvert 特定错误。我没有看到您的任何使用语句,但我认为代码如下所示。

    请注意,我正在捕获所有 MediaConvert 客户端错误,但我相信您可以通过执行 Aws\MediaConvert\Exception\MediaConvertException\NotFoundException 来专门调出 NotFoundException

    use Aws\MediaConvert\MediaConvertClient;
    use Aws\Exception\AwsException;
    use Aws\MediaConvert\Exception\MediaConvertException as MediaConvertError;
    
    
    function get_aws_job_id_status($job_id)
    {
        $result = [];
        $client = \App::make('aws')->createClient('MediaConvert', [
    
            // 'profile' => 'default',
            // 'version' => '2017-08-29',
            'region'  => 'region',
            'endpoint' => "endpoint"
        ]);
        try {
            $result = $client->getJob([
                'Id' => $job_id,
            ]);
            return $result;
        } catch (MediaConvertError $e) {
        
               /*Oh no, the job id provided ca not be found.
                 Let us log the job id and the message and return it back up to the main application
                 Note this assumes the main application is iterating through a list of JobIDs and 
                 can handle this and log that job id was not found and will not have the normal Job
                 JSON structure. 
               */
            $error = array("Id"=>$job_id, "Message"=>"Job Id Not found");
            $result = json_encode($error);
            return $result;
        }
    }
    

    另外请记住,如果您正在轮询工作状态,如果您的列表变得太大,您可能会受到限制。您需要捕获 TooManyRequestsException [1] 并尝试使用退避阈值 [2] 进行轮询。

    最具可扩展性的解决方案是使用 CloudWatch Events 并根据 STATUS_UPDATE、COMPLETE 和 ERROR 状态跟踪作业。 [3]

    [1]https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.MediaConvert.Exception.MediaConvertException.html
    [2]https://docs.aws.amazon.com/general/latest/gr/api-retries.html
    [3]https://docs.aws.amazon.com/mediaconvert/latest/ug/monitoring-overview.html

    【讨论】:

      猜你喜欢
      • 2015-04-07
      • 2013-11-22
      • 1970-01-01
      • 2017-09-17
      • 2021-11-04
      • 2014-11-20
      • 2015-08-18
      • 2014-03-14
      • 1970-01-01
      相关资源
      最近更新 更多