Friday, 13 September 2019

How to upload All type of Files on AWS S3 using Codeigniter


Upload Files on Amazon S3 Bucket using Codeigniter.

Before this,Firstly You have to know about the S3 Bucket. This is the simple Storage Service on Amazon web services .Basically It is use for storing the large amount of files and folder. We can upload the website data on aws cloud storage.
Here Bucket is simply the name of folder.And files and data inside bucket is called Objects.We can say S3 Objects. It is also consist data as well as meta data.

How to use S3 :

1. Create Amazon(AWS) S3 Account.You can use this link https://signin.aws.amazon.com then Sign in to Console.
2.Now You have access key and secret key of your aws  account. 
3.Add Aws s3 Files in your root directory of the project.
4.Create Aws3.php file inside libraries folder. and Add this code.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Description of AmazonS3
 *
 * @author wahyu widodo
 */
 include("./vendor/autoload.php");
 use Aws\S3\S3Client;
 class Aws3{
private $S3;

public function __construct(){
$this->S3 = S3Client::factory([
'key'    => 'Your Access key',
'secret' => 'Your Secret Key',
'region' => 'ap-south-1',
'signature' => 'v4'
]);
}
public function addBucket($bucketName){
$result = $this->S3->createBucket(array(
'Bucket'=>$bucketName,
'LocationConstraint'=> 'ap-south-1'));
return $result;
}
public function sendFile($bucketName, $filename){
        $result = $this->S3->putObject(array(
'Bucket' => 'connectosh',
'Key' => $bucketName.'/'.preg_replace('/\s+/', '-',$filename['name']),
'SourceFile' => $filename['tmp_name'],
'StorageClass' => 'STANDARD',
'ACL' => 'public-read'
));
return $result['ObjectURL']."\n";
}
 
 }

5.Now Add Code in your Controller Function to upload files on Aws.
         $this->load->library('aws3');
$image_data['file_name'] = $this->aws3->sendFile($sub_bucket_name,$_FILES['image']);
$data = array('upload_data' => $image_data['file_name']);

In this data variable you will get the path of uploaded files. Here you will also learn How to create Sub bucket  on S3 dynamically using Php- Codeigniter.

How to upload All type of Files on AWS S3 using Codeigniter

Upload Files on Amazon S3 Bucket using Codeigniter. Before this,Firstly You have to know about the S3 Bucket. This is the simple Storag...