Skip to content

Instantly share code, notes, and snippets.

@windweller
Last active August 29, 2015 13:56
Show Gist options
  • Save windweller/8814585 to your computer and use it in GitHub Desktop.
Save windweller/8814585 to your computer and use it in GitHub Desktop.
PHP Singleton for AWS SDK S3
<?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