我们使用带有特定前缀的ListObjects来向process返回键。有些前缀不是一直存在的。我们之前的工作环境是PHP 7.3和aws-sdk 3.142.0,代码按预期工作。如果前缀有数据,则解析结果,如果前缀不存在,则不返回任何结果。我们最近升级到PHP 8.1.8和aws-sdk 3.231.8。当前缀存在时,ListObjects按预期返回数据,如果前缀不存在,则调用失败,代码失败,并且异常代码无法处理。下面的代码处理listObjects前缀acr/,然后在acr_processed/上失败,因为前缀当前不存在于桶中。
有没有人看到这个或有一个解决方案?提前谢谢你
$prefixarray = array('acr/','acr_processed/'); // NOTE: acr_processed/ doesn't exist
foreach ($prefixarray as $prefix) {
$bucketName = 'abc';
try {
$objects = $s3->ListObjectsV2(['Bucket' => $bucketName,
'Prefix' => $prefix
]);
} catch (S3Exception $e) {
echo "there was a S3 exception";
} catch (Exception $e) {
echo 'there was an exception';
}
foreach ($objects['Contents'] as $contents) {
echo $contents['Key'] . "<br>";
}
}
问题已解决,称其为解决方法,因为当您尝试列出前缀时,如果前缀不存在,则错误500仍然返回。使用这些当前版本的PHP和Asw-sdk。
修复在列出的前缀中先创建一个dummyobject,然后再列出前缀。这个过程是HeadObject来查看dummyobject是否在那里,如果前缀中没有putObject,然后列出前缀。dummyobject将返回,但在处理列表结果时很容易跳过。
$tagstring = 'status=placeholder';
$dummykeyName = $prefix . 'placeholder.prefix';
try {
// head a s3 object:
$result = $s3->headObject([
'Bucket' => $bucketName,
'Key' => $dummykeyName
]);
// found
$NoResults = 'no';
} catch (S3Exception $e) {
die('s3 Error:' . $e->getMessage());
} catch (Exception $e) { // placeholder.prefix was not found put it
try {
$s3->putObject(
array(
'Bucket' => $bucketName,
'Key' => $dummykeyName,
'ContentType' => 'text/xml',
'Tagging' => $tagstring,
'StorageClass' => $strgclass
)
);
} catch (S3Exception $e) {
echo 'there was an S3 exception ' . $dummykeyName . '<br>';
$NoResults = 'yes';
} catch (Exception $e) {
echo 'there was an exception ' . $dummykeyName . '<br>';
$NoResults = 'yes';
}
}
// get the list of objects from the bucket
try {
$objects = $s3->ListObjectsV2(['Bucket' => $bucketName,
'Prefix' => $prefix
]);
} catch (S3Exception $e) {
$e->getMessage();
echo "<tr><td>list-enduser error" . $e . "</td></tr>";
$NoResults = 'yes';
} catch (Exception $e) {
echo 'there was an exception';
$NoResults = 'yes';
}