S3::putBucket PHP Method

putBucket() public static method

Put a bucket
public static putBucket ( string $bucket, constant $acl = self::ACL_PRIVATE, string $location = false ) : boolean
$bucket string Bucket name
$acl constant ACL flag
$location string Set as "EU" to create buckets hosted in Europe
return boolean
    public static function putBucket($bucket, $acl = self::ACL_PRIVATE, $location = false)
    {
        $rest = new S3Request('PUT', $bucket, '', self::$endpoint);
        $rest->setAmzHeader('x-amz-acl', $acl);
        if ($location !== false) {
            $dom = new DOMDocument();
            $createBucketConfiguration = $dom->createElement('CreateBucketConfiguration');
            $locationConstraint = $dom->createElement('LocationConstraint', $location);
            $createBucketConfiguration->appendChild($locationConstraint);
            $dom->appendChild($createBucketConfiguration);
            $rest->data = $dom->saveXML();
            $rest->size = strlen($rest->data);
            $rest->setHeader('Content-Type', 'application/xml');
        }
        $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::putBucket({$bucket}, {$acl}, {$location}): [%s] %s", $rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
            return false;
        }
        return true;
    }

Usage Example

Example #1
1
 function upload()
 {
     $error = "test";
     if (isset($_POST['submitbtn'])) {
         if (array_key_exists('userFile', $_FILES)) {
             if ($_FILES['userFile']['error'] === UPLOAD_ERR_OK) {
                 $filename = $_FILES["userFile"]["name"];
                 $ext = pathinfo($filename, PATHINFO_EXTENSION);
                 $allowed = array("doc", "docx", "rtf", "pdf", "txt", "odf");
                 $fname = $_FILES["userFile"]["tmp_name"];
                 // Make sure extension matches
                 if (in_array($ext, $allowed)) {
                     if ($_FILES['userFile']['size'] < 2097152) {
                         $bucket = 'sublite-resumes';
                         //Can use existing configs when merging with sublite
                         $s3 = new S3("AKIAI7IVRJCSAWFTTS7Q", "B0qzRQJ1KlLy+STC2BspwT9oZONjt+U6sRNqaRr5");
                         $s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
                         $actual_image_name = time() . '.' . $ext;
                         if ($s3->putObjectFile($fname, $bucket, $actual_image_name, S3::ACL_PUBLIC_READ)) {
                             $image = 'http://' . $bucket . '.s3.amazonaws.com/' . $actual_image_name;
                             return $image;
                         } else {
                             return "An unknown error occurred during upload!";
                         }
                         /*								// File validated; Upload the file! !!!Need to upload to S3!!!
                         								$uploaddir = 'resumes/';
                         								$uploadfile = basename($_FILES['userFile']['name']);
                         
                         								if (move_uploaded_file($_FILES['userFile']['tmp_name'], $uploaddir.$uploadfile)) {
                         								    return "File is valid, and was successfully uploaded.\n";
                         								} else {
                         								    return "An unknown error occurred during upload!";
                         								} */
                     } else {
                         $error = "Max file size exceeded!";
                     }
                 } else {
                     $error = "Bad file extension!";
                 }
             } else {
                 if ($_FILES['userFile']['error'] === UPLOAD_ERR_FORM_SIZE) {
                     $error = "Max file size exceeded!";
                 } else {
                     if ($_FILES['userFile']['error'] === UPLOAD_ERR_NO_FILE) {
                         $error = "You must choose a file!";
                     } else {
                         $error = "An unknown error occurred during upload!";
                     }
                 }
             }
             return $error;
         }
     }
 }
All Usage Examples Of S3::putBucket