S3::listBuckets PHP Method

listBuckets() public static method

Get a list of buckets
public static listBuckets ( boolean $detailed = false ) : array | false
$detailed boolean Returns detailed bucket list when true
return array | false | false
    public static function listBuckets($detailed = false)
    {
        $rest = new S3Request('GET', '', '', self::$endpoint);
        $rest = $rest->getResponse();
        if ($rest->error === false && $rest->code !== 200) {
            $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
        }
        if ($rest->error !== false) {
            self::__triggerError(sprintf("S3::listBuckets(): [%s] %s", $rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
            return false;
        }
        $results = array();
        if (!isset($rest->body->Buckets)) {
            return $results;
        }
        if ($detailed) {
            if (isset($rest->body->Owner, $rest->body->Owner->ID, $rest->body->Owner->DisplayName)) {
                $results['owner'] = array('id' => (string) $rest->body->Owner->ID, 'name' => (string) $rest->body->Owner->DisplayName);
            }
            $results['buckets'] = array();
            foreach ($rest->body->Buckets->Bucket as $b) {
                $results['buckets'][] = array('name' => (string) $b->Name, 'time' => strtotime((string) $b->CreationDate));
            }
        } else {
            foreach ($rest->body->Buckets->Bucket as $b) {
                $results[] = (string) $b->Name;
            }
        }
        return $results;
    }

Usage Example

Example #1
0
 /**
  * Creates bucket
  *
  * @param string $container_id
  * @param string $error
  * @return boolean
  */
 function create_container(&$container_id, &$error)
 {
     if (!$this->_init($error)) {
         return false;
     }
     $this->_set_error_handler();
     $buckets = @$this->_s3->listBuckets();
     if ($buckets === false) {
         $error = sprintf('Unable to list buckets (%s).', $this->_get_last_error());
         $this->_restore_error_handler();
         return false;
     }
     if (in_array($this->_config['bucket'], (array) $buckets)) {
         $error = sprintf('Bucket already exists: %s.', $this->_config['bucket']);
         $this->_restore_error_handler();
         return false;
     }
     if (empty($this->_config['bucket_acl'])) {
         $this->_config['bucket_acl'] = S3::ACL_PRIVATE;
     }
     if (!isset($this->_config['bucket_location'])) {
         $this->_config['bucket_location'] = S3::LOCATION_US;
     }
     if (!@$this->_s3->putBucket($this->_config['bucket'], $this->_config['bucket_acl'], $this->_config['bucket_location'])) {
         $error = sprintf('Unable to create bucket: %s (%s).', $this->_config['bucket'], $this->_get_last_error());
         $this->_restore_error_handler();
         return false;
     }
     $this->_restore_error_handler();
     return true;
 }
All Usage Examples Of S3::listBuckets