<?php

require APPPATH . 'third_party/aws/autoloader.php';

use Aws\S3\S3Client;

class Awsfileuploader
{

    private $CI;
    private $bucket = 'cm-img';
    private $accessKey = 'AKIA2WGASVZ5TQZDCANX';
    private $secretAccessKey = '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';
    private $s3;

    public function __construct()
    {
        $this->CI = &get_instance();  
        $this->CI->load->helper('string');
        $credentials = new Aws\Credentials\Credentials($this->accessKey, $this->secretAccessKey);
        $this->s3 = new S3Client(array(
            'version' => 'latest',
            'region' => 'ap-northeast-2',
            'credentials' => $credentials,
            'scheme' => 'http',
        ));      
    }

    public function upload($filePath, $keyPath)
    {
        $result = $this->s3->putObject(array(
            'Bucket' => $this->bucket,
            'Key' => $keyPath,
            'Body' => fopen($filePath, 'r'),
            'ACL' => 'public-read',
        ));
        @unlink($filePath);
        return  '/'. $keyPath;
    }

    public function delete($keyPath) {
        $result = $this->s3->deleteObject(array(
            'Bucket' => $this->bucket,
            'Key' => $keyPath
        ));
    }
}
