Last active
August 29, 2015 13:56
-
-
Save allenanie/8814585 to your computer and use it in GitHub Desktop.
PHP Singleton for AWS SDK S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
require_once 'vendor/autoload.php'; | |
use Aws\S3\S3Client; | |
final class AWS_S3 | |
{ | |
private static $client = null; | |
private function __construct() { | |
} | |
// call this method to get singleton | |
public static function instance() | |
{ | |
static $instance = null; | |
if (self::$client === null) { | |
self::$client = S3Client::factory(array( | |
'key' => 'PUT IN YOUR KEY', | |
'secret' => 'PUT IN YOUR SECRET KEY' | |
)); | |
$instance = new AWS_S3(); | |
} | |
return $instance; | |
} | |
public function get_client() | |
{ | |
return self::$client; | |
} | |
//first should be $_FILES['file']['tmp_name'], second should be $_FILES['file']['name'] | |
public function save($file, $file_name) | |
{ | |
$bucket = "Your bucket Name!"; | |
if (!$s3Client->doesBucketExist($bucket)) { | |
$result = $s3Client->createBucket(array( | |
'Bucket' => $bucket | |
)); | |
$s3Client->waitUntilBucketExists(array('Bucket' => $bucket)); | |
} | |
$file_name = uniqid().'/'.$file_name; | |
$result = $s3Client->putObject(array( | |
'Bucket' => $bucket, | |
'Key' => $file_name, | |
'SourceFile' => $file, | |
'ACL' => 'public-read' | |
)); | |
return $result; | |
} | |
} | |
/* End of file AWS_S3.php */ | |
/* Location: ./application/models/AWS_S3.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment