<?php
require_once ROOT_PATH . '/vendor/autoload.php';
class CosApi {
	private $region = 'ap-guangzhou';
	private $appid = 1251449951;
	private $secretId = 'AKIDMXTAjEKZM3g4GDvnHVTQMwGLfmjQ9KRI';
	private $secretKey = 'wcw0qQvARefTLzMVWhiIi1FyUq1xjYCk';
	private $url = 'https://smart-1251449951.cos.ap-guangzhou.myqcloud.com';
    private $bucket = 'smart-1251449951';
    private $cosClient = null;

	public function __construct() {
		$this->cosClient = new Qcloud\Cos\Client( 
            array(
                'region' => $this->region,
                'credentials' => array(
                    'appId' => $this -> appid,
                    'secretId' => $this -> secretId,
                    'secretKey' => $this -> secretKey, 
                ), 
            )
        );
	}

	// 上传文件流
	public function putFileStream($local_path) {
        $key = "./cms_anyp/" . date("Y", time()) . "/" . date("m", time()) . "/" . date("d", time()) . '/' . basename($local_path);
		$result = $this->cosClient->putObject(
            array(
                'Bucket' => $this->bucket, 
                'Key' => $key, 
                'Body' => fopen($local_path, 'rb'
            ))
        );
		if ($result) {
			$url = $this -> url . ltrim($key, '.');
			return $url;
		} else {
			return false;
		}
	}
}
