You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.6 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace Admin\Controller;
use Think\Controller;
class CosController extends Controller {
public function cosupload($src,$dst,$d=1){
Vendor('COS5.vendor.autoload');
$schema=C("cos_storage.schema")?:"https";
$secretId = C("cos_storage.SecretId"); //"云 API 密钥 SecretId";
$secretKey = C("cos_storage.SecretKey"); //"云 API 密钥 SecretKey";
$region = C("cos_storage.domain"); //设置一个默认的存储桶地域
$cosClient = new \Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => $schema, //协议头部默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey
)
)
);
$bucket = C("cos_storage.bucket"); //存储桶名称 格式BucketName-APPID
$key = $dst;
$srcPath = $src;//本地文件绝对路径
try {
$result = $cosClient->Upload(
$bucket = $bucket,
$key = $key,
$body = fopen($srcPath, 'rb')
);
$location = $result['Location'];
if(!preg_match('/^(http)|(https)/', $location)) {
$result['Location'] = 'https://' . $result['Location'];
}
return $result['Location'];
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
}