北方地区上传报错,incorrect zone, please use up-z1.qiniu.com
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)];
}
赞赏码
非学,无以致疑;非问,无以广识