Create Folder in AWS S3 Bucket Using PHP Code


Aftab Muni

aws

To create folder in AWS S3 bucket, first you need is S3 connection object.
Now we will create S3 object:

try {
        $s3Client = S3Client::factory(array(
            'key'    => API_KEY,
            'secret' => API_SECRET
        ));
    } catch (S3Exception $e) {
        echo CONNECT_S3;
    }

NOTE: We will put connection code in try catch to know the error if occurred.

Now we will create folder using following:

$s3Client->putObject(array( 
   'Bucket'       => BUCKET, // Defines name of Bucket
   'Key'          => "pictures/", //Defines Folder name
   'Body'       => "",
   'ACL'          => 'public-read' // Defines Permission to that folder
));

Here if there is no such folder in the given bucket, then it will create folder in it or else it will not.

View original post