<?php
require FCPATH . "vendor/aws_v3/aws-autoloader.php";

date_default_timezone_set("Asia/Jakarta");

class Sas_s3
{
    var $bucket;
    var $s3;
    var $endpoint = "https://is3.cloudhost.id";
    function __construct()
    {
        $this->bucket = "audio-sample";
        $this->s3 = new Aws\S3\S3Client([
            "region"  => "us-east-1",
            "endpoint" => $this->endpoint,
            "use_path_style_endpoint" => true,
            "credentials" => [
                "key" => "DLBFBBB8R22W4B5IE7PC",
                "secret" => "byCv726DN1TCQvLVHdSLMJg3K0i3Ajhm4rXPAVCu"
            ]
        ]);
    }
    function create_bucket($bucket)
    {
        $result = $this->s3->createBucket(["Bucket" =>  $bucket]);
        return $result;
    }
    function upload($bucket, $key, $contentType, $body)
    {
        $result = $this->s3->putObject(
            [
                "Bucket" => $bucket,
                "Key" => $key,
                "Body" => $body,
                "ContentType" => $contentType
            ]
        );
        return $result;
    }
    // avail : 10 minutes
    function show_url($bucket, $key, $avail)
    {
        $cmd = $this->s3->getCommand("GetObject", ['Bucket' => $bucket, 'Key' => $key]);
        $request = $this->s3->createPresignedRequest($cmd, '+20 minutes');
        $presignedUrl = $request->getUri();
        return $presignedUrl;
    }
}
