<?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([
			'version' => 'latest',
            'region'  => 'ap-northeast-1',
            'credentials' => array(
                'key' => 'AKIAZLDAZMGCLZ3376VG',
                'secret' => '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'
            ),
			'Bucket'=> 'mymakeabl-data'
		]);
	}	
	
	public function addBucket($bucketName){
		$result = $this->S3->createBucket(array(
			'Bucket'=>$bucketName,
			'LocationConstraint'=> 'ap-northeast-1'));
		return $result;	
	}
	
	public function sendFile($bucketName, $filename){
		$result = $this->S3->putObject(array(
				'Bucket' => $bucketName,
				'Key' => $filename['name'],
				'SourceFile' => $filename['tmp_name'],
				'ContentType' => 'image/png',
				'StorageClass' => 'STANDARD',
				'ACL' => 'public-read'
		));
		
		return $result['ObjectURL']."\n";
	}		
	
	public function listobject($prefix){
	 
       $result = $this->S3->listObjects([
                'Bucket' => 'mymakeabl-data',
                'Prefix' => $prefix
            ]);
        $data = [];
         foreach ($result['Contents'] as $object) {
            if (strpos($object['Key'] . PHP_EOL,"_qr") == false) {
                array_push($data,$object['Key'] . PHP_EOL);
            }
        }
        
        
        return array_reverse($data);
	}
	 
	 
 }