csrb/api/application/libraries/Aws_s3.php
2020-02-15 13:38:20 +05:30

70 lines
1.7 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH .'vendor/aws-sdk/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
class aws_s3
{
private $S3Client;
function __construct()
{
// Construct the parent class
// parent::__construct();
$s3_credentials = array( 'credentials' => array('key' => SCHOOL_AWS_KEY,'secret' => SCHOOL_AWS_SECRET), 'version' => 'latest','region' => 'ap-south-1' );
$this->S3Client = S3Client::factory($s3_credentials);
}
public function get_s3_url($bucket_name = null,$key)
{
try
{
$cmd = $this->S3Client->getCommand('GetObject', ['Bucket' => ($bucket_name ? $bucket_name :SCHOOL_BUCKET_NAME ),'Key'=> $key ]);
$request = $this->S3Client->createPresignedRequest($cmd, '+5 minutes');
$presignedUrl = (string) $request->getUri();
return $presignedUrl;
}
catch(AwsException $e)
{
log_message('ERROR',$e->getMessage());
return $e->getMessage();
}
}
public function uploadFileToS3Bucket($data)
{
// echo 'UPLAOD';
// print_r($data);//;die();
$result = "";
try {
$result = $this->S3Client->putObject([
'Bucket' => $data['bucket_name'],
'Key' => $data['key'],
'SourceFile' => $data['sourcefile'],
'ContentType' => '*/*',
'StorageClass' => 'STANDARD',
'ACL'=>'private'
]);
} catch (AwsException $e)
{
$result = false;
log_message('ERROR',$e);
}
return $result;
}
}