简体   繁体   中英

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.

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.

How can I know, when catching S3Exception, if the exception relates to a non-existing key?

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). You can use the getAwsErrorCode() method of the exception to check for specific error codes. This is functionally equivalent to catching different exception classes, but provides that function without adding bloat to the SDK.

And the list of error codes for S3 , which shows that the one I'm looking for is NoSuchKey .

So the new way to catch this error is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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