gpt4 book ai didi

php - 无法捕获 AWS S3 异常

转载 作者:可可西里 更新时间:2023-11-01 13:46:56 24 4
gpt4 key购买 nike

我正在尝试通过以下代码使用 Stream API 获取 S3 对象的文件大小:

try{
$fileSize = filesize("s3://".$bucket."/".$filename);
}catch(Aws\S3\Exception\NoSuchKeyException $e) {
return false;
}

如果 key 不存在,我会收到以下错误:

[Tue Oct 13 23:03:32 2015] [error] [client 54.225.205.152] PHP Warning: File or directory not found: s3://mybucket/myfile.jpg in /var/www/vendor/aws/aws-sdk-php/src/Aws/S3/StreamWrapper.php on line 774

[Tue Oct 13 23:03:32 2015] [error] [client 54.225.205.152] PHP Warning: filesize(): stat failed for s3://mybucket/myfile.jpg in /var/www/api-dev/awsFunc.php on line 278

[Tue Oct 13 23:03:32 2015] [error] [client 54.225.205.152] PHP Fatal error: Uncaught Aws\S3\Exception\NoSuchKeyException: AWS Error Code: NoSuchKey, Status Code: 404, AWS Request ID: 4A6F1372301D02F7, AWS Error Type: client, AWS Error Message: The specified key does not exist., User-Agent: aws-sdk-php2/2.8.21 Guzzle/3.9.3 curl/7.22.0 PHP/5.3.10-1ubuntu3.19\n thrown in /var/www/vendor/aws/aws-sdk-php/src/Aws/Common/Exception/NamespaceExceptionFactory.php on line 91

因此,尽管我明确 try catch Aws\S3\Exception\NoSuchKeyException,系统还是会抛出它。

更新:

我发现了错误。异常应该命名空间应该以 '\' 而不是 Aws 开头,像这样:

try{
$fileSize = filesize("s3://".$bucket."/".$filename);
}catch(\Aws\S3\Exception\NoSuchKeyException $e) {
return false;
}

我不知道为什么当我使用 命名空间时,命名空间不以“\”开头,但在异常(exception)情况下它需要它。我希望有人解释一下。

最佳答案

好的,这对我有用:

use Aws\S3\Exception\S3Exception as S3;


try {
$podcast = $this->uploadFileToS3($request);
} catch(S3 $e) {
return $e->getMessage();
}

在我的例子中,我将消息传递给 session 闪存,如下所示:

 return redirect('dashboard/episode/create')->with('status', $e->getMessage());

所以这一切都取决于你想如何使用它。

try {
$fileSize = filesize("s3://".$bucket."/".$filename);
} catch(S3 $e) {
return $e->getMessage();
}

祝你好运!

关于php - 无法捕获 AWS S3 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33113987/

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