
define('S3_Development_Key', $SubmittedData['Collection_389_2'] );
define('S3_Development_Secret', $SubmittedData['Collection_389_3']);
require_once $_SERVER['DOCUMENT_ROOT'].'/classes/StorageS3Amazon/sdk.class.php';

$BucketName = $SubmittedData['Collection_389_4'];

try {
						
						$s3 = new AmazonS3();
						
						$response = $s3->get_bucket_list();
						
						if (count($response) == 0)
						{
								$ruleErrors[] = 'There was an error connecting to S3, bucket name ' . addslashes($BucketName) . '. Please check the keys and bucket name.';
						}
						else
						{
							$foundBucket = 0;
							foreach ($response as $k => $v)
							{
										if (strtolower($v) == strtolower($BucketName))
										{
												$foundBucket = 1;
										}
							}
							
							if ($foundBucket == 0)
							{
									//we try to create the bucket
									//check if exists first
									try {
											
											$response = $s3->if_bucket_exists($BucketName);
																						
											if (!$response)
											{
													//we try to create the bucket
													$response = $s3->create_bucket($BucketName, AmazonS3::REGION_US_E1);
													if (!$response->isOK())
													{
															$ruleErrors[] = 'There was an error connecting to S3. Failed creating bucket ' . addslashes($BucketName) . '. ';
													}
											}
											
											
									}
									catch(Exception $ex) {
											
											$ruleErrors[] = 'There was an error connecting to S3. The bucket name ' . addslashes($BucketName) . ' is not a valid bucket name. ';
									}				
									
									
							}
							
						}
						
												
}						
catch(Exception $ex) {
				
						$ruleErrors[] = 'There was an error connecting to S3, bucket name ' . addslashes($BucketName) . '. Please check the keys and bucket name.';
}			