Amazon_S3_And_CloudFront::create_bucket PHP Method

create_bucket() public method

Create an S3 bucket
public create_bucket ( string $bucket_name, null | string $region = null ) : boolean | WP_Error
$bucket_name string
$region null | string option location constraint
return boolean | WP_Error
    function create_bucket($bucket_name, $region = null)
    {
        try {
            $args = array('Bucket' => $bucket_name);
            if (defined('AS3CF_REGION')) {
                // Make sure we always use the defined region
                $region = AS3CF_REGION;
            }
            if (!is_null($region) && self::DEFAULT_REGION !== $region) {
                $args['LocationConstraint'] = $region;
            }
            $this->get_s3client()->createBucket($args);
        } catch (Exception $e) {
            return new WP_Error('exception', $e->getMessage());
        }
        return true;
    }

Usage Example

 /**
  *
  */
 public function rooftop_add_s3_bucket(&$error = null)
 {
     $bucket = 'rooftop.site.' . get_current_blog_id();
     $bucket = apply_filters('as3cf_setting_bucket', $bucket);
     $amazon_webservices_path = ABSPATH . '../app/mu-plugins/amazon-web-services/amazon-web-services.php';
     $wp_s3_cloudfront_path = ABSPATH . '../app/mu-plugins/amazon-s3-and-cloudfront/wordpress-s3.php';
     $webservice = new Amazon_Web_Services($amazon_webservices_path);
     $aws = new Amazon_S3_And_CloudFront($wp_s3_cloudfront_path, $webservice);
     try {
         $aws->create_bucket($bucket, 'eu-west-1');
         return true;
     } catch (Exception $e) {
         error_log('Error creating bucket S3: ' . $e->getMessage());
         $error = $e;
         return;
     }
 }
Amazon_S3_And_CloudFront