Jump to content

Preventing a warning message from showing


galvin

Recommended Posts

I hope this is enough info, but I have this code (at the bottom) that grabs images from my S3 account (Amazon Web Services)... If there is no bucket for the name given, it gives this warning...

 

Warning: S3::getBucket(): [NoSuchBucket] The specified bucket does not exist in /home/xxxxxxx/public_html/xxxxxxx.com/S3.php on line 126

 

Assuming it's not specific syntax to S3 and is general PHP syntax, how could I prevent that warning from showing, by saying in plain English "If a bucket by that name doesn't exist, I'm gonna do something else and NOT spit out this warning?"

 

Basically, I want to echo a message like "There is no bucket yet" instead of having that ugly warning.

 

 

// Get the contents of our bucket
$contents = $s3->getBucket("mybucket");
foreach ($contents as $file){

	$fname = $file['name'];
	$furl = "http://mybucket.s3.amazonaws.com/".$fname;

	//output a link to the file
	echo "<a href=\"$furl\">$fname</a><br />";
	echo "<img src='$furl' width='50' /><br />";
	echo "$furl<br />";

}

Generally functions or methods should return either true or false depending on wether or not they fail or succeed. This makes them easy to handle using logic like:

 

if ($contents = $s3->getBucket("mybucket")) {
  foreach ($contents as $file) {
    // rst of code
  }
}

 

Clearly, whatever $s3 is pretty poorly written as it appears to be triggering a warning. I would investigate the getBucket() method itself and have it either return true or false, or if really necessary an exception. At least they are easy to handle.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.