<?php
//腾讯云链接用
namespace app\common\server;

use Qcloud\Cos\Client;

class cosserver
{
    public function __construct()
    {
        require EXTEND_PATH . '/tencent-php/vendor/autoload.php';
    }
    public function getconfig()
    {
        $data = [
            'secretId' => 'AKID2uVrwjGXWEfpjMjqhJejY6ML2TdV6SXL',
            'secretKey' => 'Yqrb3EH4ZnlSs2xaw1qOffNoyrSnrfCU',
            'region' => "ap-nanjing",
        ];
        return $data;
    }
    public function uploadfiles($path, $dir, $filename, $bucket)
    {
        $config = $this->getconfig();
        //$dirname = "workcard/" . $dir;
        $key = $dir . '/' . $filename;
        $cosClient = new Client(
            array(
                'region' => $config['region'],
                'schema' => 'https', //协议头部，默认为http
                'credentials' => array(
                    'secretId' => $config['secretId'],
                    'secretKey' => $config['secretKey'],
                ),
            )
        );
        //key可以设置路径
        try {
            $result = $cosClient->putObject(array(
                //'Bucket' => 'backup-1306866048', //格式：BucketName-APPID
                'Bucket' => $bucket,
                'Key' => $key,
                'Body' => fopen($path, 'rb'),
                //'Body' => $base64,
            ));
            // 请求成功
            $path = $result['Location'];
            return backarr(1, 'success', ['path' => 'http://' . $path]);
        } catch (\Exception $e) {
            // 请求失败
            return backarr(0, $e->getMessage());
        }
    }
}
