一、https://www.qiniu.com/用qq或者微信登入七牛雲php
二、添加一個對象存儲微信
三、在我的中心獲取appkey和appsecretapp
四、綁定一個域名dom
五、把sdk放在本身的項目中ui
封裝一個上傳的類this
<?php require 'vendor/autoload.php'; class qiniuYunInit { /** * @var string */ private $access_key; /** * @var string */ private $secret_key; /** * 域名 * @var string */ private $domain = ""; /** * 配置祕鑰對 * @param array $config */ public function setConfig($config) { $this->access_key = $config['access_key']; $this->secret_key = $config['secret_key']; $this->domain = $config['domain']; } /** * 獲取上傳圖片的憑證 * @param string $bucket 空間名 * @param string $resource_key 資源名稱 * @param int $deadline token有效時間 * @param array $opts 自定義參數 * 'insertOnly'=>1, 只能新增圖片 * 'mimeLimit'=>'image/*', 只容許上傳圖片 * * @return string */ public function getUploadToken($bucket, $resource_key, $deadline = 3600, $opts = null) { $auth = new \Qiniu\Auth($this->access_key, $this->secret_key); return $auth->uploadToken($bucket, $resource_key, $deadline, $opts); } /** * 直傳文件到七牛雲 * @param string $upload_token * @param string $key * @param string $filename * @return array */ public function uploadFile($upload_token, $key, $filename) { $uploader = new \Qiniu\Storage\UploadManager(); return $uploader->putFile($upload_token, $key, $filename); } /** * 獲取公共空間的資源訪問連接 http://p7sqxug2z.bkt.clouddn.com/ * @param string $key 資源名 * @param string $scheme * @return string */ public function getPublicResourceUrl($key, $scheme = 'http') { $key = ltrim($key, "/"); return "{$this->domain}/{$key}"; } /** * 獲取訪問資源的域名 * @return string */ public function getVisitDomain($scheme = 'http') { return "{$scheme}://{$this->domain}/"; } }
代碼中這樣寫url
/** * 圖片上傳到七牛雲 * @param string $tmpLocation 臨時圖片地址 絕對地址,/www/initphp/www/upload/temp/xxx.jog * @param string $savePath 美圖雲文件的存儲位置 * @param string $fileType 文件類型 * @return string 上傳到美圖雲後的地址 * */ protected function uploadToQNCloud($tmpLocation, $savePath = 'test/' ,$fileType = 'image/*') { if (is_file($tmpLocation)) { $config = InitPHP::getConfig(); $this->getQiniuyun()->setConfig($config['qiniu_yun']); $path_data = pathinfo($tmpLocation); $savePath = trim($savePath, "/");//去掉兩邊的"/" $key = $savePath . '/' . date("Ymd") . '/' . substr(md5(getRandoms(4) . uniqid()), 0, 10) . $path_data['basename']; $uploadToken = $this->getQiniuyun()->getUploadToken($config['qiniu_yun']['bucket'], $key, 10, array( 'insertOnly' => 1, //只能新增圖片 'mimeLimit' => $fileType, // 只容許上傳圖片 )); $this->getQiniuyun()->uploadFile($uploadToken, $key, $tmpLocation); $url = $this->getQiniuyun()->getPublicResourceUrl($key); @unlink($tmpLocation); return $url; } }
/** * @return qiniuYunInit|object */ private function getQiniuYun() { return InitPHP::getLibrarys('qiniuYun'); }
配置信息寫在本身的配置文件中。spa
注意:一、上線時在七牛雲上要配置域名,否則cdn無效,並且會限制訪問速度等等.net
https://blog.csdn.net/yanyan42/article/details/806846143d