大家好,又见面了,我是全栈君。
Qiniu.php 文件中,两个重要方法
/**
* Qiniu constructor.
* @param $accessKey
* @param $secretKey
* @param $domain
* @param string $bucket
* @param string $zone 默认华南机房
*/
function __construct($accessKey, $secretKey, $domain, $bucket, $zone = 'south_china')
{
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
$this->domain = $domain;
$this->bucket = $bucket;
$config = $this->zoneConfig($zone);
$this->up_host = $config['up'][0]; //读取第一个可用线路
}
protected function zoneConfig($key = null)
{
$arr = [
//华东
'east_china' => [
'up' => array("up.qiniup.com", 'up-nb.qiniup.com', 'up-xs.qiniup.com'),
],
//华北
'north_china' => [
'up' => array('up-z1.qiniup.com'),
],
//华南机房
'south_china' => [
'up' => array('up-z2.qiniup.com', 'up-gz.qiniup.com', 'up-fs.qiniup.com'),
],
//北美机房
'north_america' => [
'up' => array('up-na0.qiniup.com'),
]
];
if ($key !== null) {
if (isset($arr[$key])) {
return $arr[$key];
} else {
throw new \Exception('区域不存在');
}
}
return $arr;
}
默认使用的是华南机房;所以在调用的时候,改用华北机房就好
$qiniu = new Qiniu(Product::AK, Product::SK, Product::DOMAIN, Product::BUCKET,'north_china');
//上传图片
public function upload()
{
if ($_FILES['Product']['error']['cover'] > 0) {
return false;
}
$qiniu = new Qiniu(Product::AK, Product::SK, Product::DOMAIN, Product::BUCKET,'north_china');
$key = uniqid();
$qiniu->uploadFile($_FILES['Product']['tmp_name']['cover'], $key);
$cover = $qiniu->getLink($key);
$pics = [];
foreach ($_FILES['Product']['tmp_name']['pics'] as $k => $file) {
if ($_FILES['Product']['error']['pics'][$k] > 0) {
continue;
}
$key = uniqid();
$qiniu->uploadFile($file, $key);
$pics[$key] = $qiniu->getLink($key);
}
return ['cover' => $cover, 'pics' => json_encode($pics)];
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/111583.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...