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.

Configuring AWS SDK with PHP

AmazonWebservices_Logo.svg
The AWS SDK for PHP can be configured in many ways to suit your needs. This guide highlights the use of configuration files with the service builder as well as individual client configuration options.

The simple way is as follows:

First we need to import AWS SDK  into our project and than we will do following,

require './AWS/aws-autoloader.php';

use Aws\S3\S3Client;
use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\Model\MultipartUpload\UploadBuilder;
use Aws\S3\Exception\S3Exception; 

Now we will simply use below code to get connection with S3 Storage

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

For more information you can visit AWS Configuration with PHP

Upload File to Amazon Cloud Server using AWS

To upload any file to amazon EC2  S3 cloud server using PHP code you simply need to include AWS package(SDK) and include necesary files into your code.

Below is the function to upload file directly to S3 Server:

function uploadFileToS3($file, $file_name){
// Here $file is a $_FILE array
// and $file_name is the name of file you need to create
try {
$s3Client = S3Client::factory(array(
‘key’ => API_KEY, // defines your API KEY of EC2
‘secret’ => API_SECRET // defines your API SECRET of EC2
));
} catch (S3Exception $e) {
echo CONNECT_S3;
}

$filename = “uploads/files/” . $file_name;
$uploader = UploadBuilder::newInstance()
->setClient($s3Client)
->setSource($file[‘image’][‘tmp_name’])
->setBucket(‘my-bucket’)
->setKey($filename)
->setMinPartSize(5 * 1024 * 1024)
->setOption( ‘ContentType’, $file[‘image’][‘type’])
->setOption(‘ACL’, ‘public-read’)
->setConcurrency(3)
->build();
$uploader->upload();
return true;
}

 

Now I will simply call the above function by passing 2 arguments i.e. $_FILES array and the file name which you need to create on S3 server.

So,

$file_name = ‘my-new-image.png’;

$response = uploadFileToS3($_FILES, $file_name);

NOTE: Do not  forgot to include AWS package(SDK) on the top of your code.

You can download aws.zip  from AWS Github