gpt4 book ai didi

amazon-web-services - 处理 AWS PHP SDK 2 中的错误

转载 作者:行者123 更新时间:2023-12-04 10:19:06 25 4
gpt4 key购买 nike

如果我尝试从我的 S3 存储桶中获取一个不存在的对象,Amazon PHP SDK 2 会给我一个非常丑陋的错误。对我来说很方便,但对最终用户没有任何意义......

例如:

$s3 = $aws->get('s3');

$result = $s3->getObject(array(
'Bucket' => 'my bucket',
'Key' => 'path/to/file'
));

错误:

Fatal error: Uncaught Aws\S3\Exception\NoSuchKeyException: AWS Error Code: NoSuchKey, Status Code: 404, AWS Request ID: xxxxxxxxxxxxx, AWS Error Type: client, AWS Error Message: The specified key does not exist. thrown in AWS/vendor/aws/aws-sdk-php/src/Aws/Common/Exception/NamespaceExceptionFactory.php on line 89



有没有一种方法可以确定是否存在错误并打印出有意义的消息而不是上述消息?

最佳答案

我突然想到要试试这个:

try {
$result = $s3->getObject(array(
'Bucket' => 'my bucket',
'Key' => 'path/to/file'
));
} catch (Exception $e) {
// I can put a nicer error message here
}

关于amazon-web-services - 处理 AWS PHP SDK 2 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15918746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com