简体   繁体   English

AWS SDK for PHP v3中是否存在与NoSuchKeyException等效的内容?

[英]Is there an equivalent to NoSuchKeyException in AWS SDK for PHP v3?

AWS SDK v2 used to have a specific NoSuchKeyException , which is gone in v3. AWS SDK v2曾经有一个特定的NoSuchKeyException ,它在v3中消失了。

This was the way then to catch a non-existing key error: 这是捕获不存在的键错误的方法:

try {
    $s3Client->getObject([
        'Bucket' => $bucket,
        'Key'    => $key
    ]);
} catch (NoSuchKeyException $e) {
    // ...
}

The only exception thrown now is S3Exception , which does not have a similar sub-class. 现在抛出的唯一异常是S3Exception ,它没有类似的子类。

How can I know, when catching S3Exception, if the exception relates to a non-existing key? 在捕获S3Exception时,如果异常与不存在的密钥相关,我怎么知道?

Is there a specific exception code, and if so, where to find the list of such codes? 是否有特定的异常代码,如果有,在哪里可以找到这些代码的列表?

Just found the reason in the migration guide : 刚刚在迁移指南中找到了原因:

You should handle errors by catching the root exception class for each service (eg, Aws\\Rds\\Exception\\RdsException). 您应该通过捕获每个服务的根异常类来处理错误(例如,Aws \\ Rds \\ Exception \\ RdsException)。 You can use the getAwsErrorCode() method of the exception to check for specific error codes. 您可以使用异常的getAwsErrorCode()方法来检查特定的错误代码。 This is functionally equivalent to catching different exception classes, but provides that function without adding bloat to the SDK. 这在功能上等同于捕获不同的异常类,但提供该功能而不向SDK添加膨胀。

And the list of error codes for S3 , which shows that the one I'm looking for is NoSuchKey . 以及S3的错误代码列表 ,显示我正在寻找的是NoSuchKey

So the new way to catch this error is: 因此,捕获此错误的新方法是:

try {
    $s3Client->getObject([
        'Bucket' => $bucket,
        'Key'    => $key
    ]);
} catch (S3Exception $e) {
    if ($e->getAwsErrorCode() == 'NoSuchKey') {
        // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM