Create Folder in AWS S3 Bucket Using PHP Code

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.

Advertisements

One thought on “Create Folder in AWS S3 Bucket Using PHP Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s