In my phpunit tests, I can successfully get a NoSuchKey error when a object doesn't exist. i.e. this works fine:
try {
$s3->getObject([
'Bucket' => $mybucket,
'Key' => 'does/not/exist.jpg',
]);
} catch (Aws\S3\Exception\S3Exception $e) {
$this->assertEquals('NoSuchKey',$e->getAwsErrorCode());
}
But the same thing with the SaveAs parameter does not give the same error code, i.e.
try {
$s3->getObject([
'Bucket' => $mybucket,
'Key' => 'does/not/exist.jpg',
'SaveAs' => '/tmp/whatever.jpg',
]);
} catch (Aws\S3\Exception\S3Exception $e) {
$this->assertEquals('NoSuchKey',$e->getAwsErrorCode()); // This FAILS
}
$e->getAwsErrorCode() is null. Doing getMessage() gives:
Aws\S3\Exception\S3Exception: Error executing \"GetObject\" on \"https://mybucket.s3-us-west-2.amazonaws.com/does/not/exist.jpg\"; AWS HTTP error: Client error response [url] https://mybucket.s3-us-west-2.amazonaws.com/does/not/exist.jpg [status code] 404 [reason phrase] Not Found Unable to parse error information from response - Error parsing XML: String could not be parsed as XML in /code/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php on line 192.
Versions:
php -vWhat version of the PHP SDK are you using? And what is the output of running php -m at the terminal?
I am unable to reproduce this issue on v3.33.1 of the SDK, as both properly assert that error code.
$excepted = false;
try {
$s3->getObject([
'Bucket' => $bucket,
'Key' => $missingFile,
]);
} catch (\Aws\S3\Exception\S3Exception $e) {
$excepted = true;
Assert::assertEquals('NoSuchKey', $e->getAwsErrorCode());
} finally {
Assert::assertTrue($excepted);
}
$excepted = false;
try {
$s3->getObject([
'Bucket' => $bucket,
'Key' => $missingFile,
'SaveAs' => $sinkPath
]);
} catch (\Aws\S3\Exception\S3Exception $e) {
$excepted = true;
Assert::assertEquals('NoSuchKey', $e->getAwsErrorCode());
} finally {
Assert::assertTrue($excepted);
}
Thanks for replying and doing a test yourself.
I'm not sure why we are getting different results. I'm using a older version of guzzle, I wonder if that is it:
"guzzlehttp/guzzle": "5.3.1",
I updated the SDK to v3.33.1 with the same results.
I updated my issue to include versions and put them here too:
php -v
PHP 7.0.17 (cli) (built: Mar 17 2017 22:49:25) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.17, Copyright (c) 1999-2017, by Zend Technologies
with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
php -m
[PHP Modules]
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
geoip
hash
iconv
imagick
json
libxml
mbstring
mcrypt
memcached
mongodb
mysqlnd
newrelic
OAuth
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
soap
solr
SPL
sqlite3
standard
tokenizer
xdebug
xml
xmlreader
xmlwriter
Zend OPcache
zip
zlib
[Zend Modules]
Xdebug
Zend OPcache
I can confirm when I upgrade guzzlehttp/guzzle from 5.3.1 to 6.3.0 the problem goes away -
i.e. NoSuchKey is returned from the getAwsErrorCode() function call when the SaveAs param is used in getObject().
Unfortunately, I cannot upgrade guzzle in my app at this time so I'm stuck with this bug.
Feel free to close this Issue if you deem it unimportant.
I'm able to reproduce your issue when I move down to guzzlehttp/guzzle 5.3.1. Looks like this is a bug in the way we're handling SaveAs/sink/save_to in the Guzzle v5 GuzzleHandler. We'll be posting a PR in the near future to resolve this issue.
@tomfotherby #1359 has a potential fix for this issue if you'd like to take a look.
Your fix solved my issue, thank you, looking forward to it being merged.