public function getList() { $size = $this->request->get('size', $this->listSize); $start = $this->request->get('start', ''); $auth = new Auth(config('ueditor.core.qiniu.accessKey'), config('ueditor.core.qiniu.secretKey')); $bucketManager = new BucketManager($auth); list($items, $marker, $error) = $bucketManager->listFiles(config('ueditor.core.qiniu.bucket'), $this->path, $start, $size); if ($error) { return ["state" => $error->message(), "list" => array(), "start" => $start, "total" => 0]; } if (empty($items)) { return ["state" => "no match file", "list" => array(), "start" => $start, "total" => 0]; } $files = []; foreach ($items as $v) { if (preg_match("/\\.(" . $this->allowFiles . ")\$/i", $v['key'])) { $files[] = array('url' => rtrim(config('ueditor.core.qiniu.url'), '/') . '/' . $v['key'], 'mtime' => $v['mimeType']); } } if (empty($files)) { return ["state" => "no match file", "list" => array(), "start" => $start, "total" => 0]; } /* 返回数据 */ $result = ["state" => "SUCCESS", "list" => $files, "start" => $start, "total" => count($files)]; return $result; }
/** * 上传头像 * 图片上传成功后做标记,渲染裁剪功能,裁剪后坐标传送给Headcut,Headcut进行裁剪,保存图片,标记。 */ public function actionIndex() { $accessKey = 'l5Y69nZUNTSeeqJx1LqCJP1KuLWIGU_3JVZXLzN-'; $secretKey = '6-4HOEBkNlJpo_TuFrM9W8ZEjytRZAYjuiG0F8Df'; $auth = new Auth($accessKey, $secretKey); $bucketMgr = new BucketManager($auth); $bucket = 'colfans-uploads-public'; $key = 'fun.jpg'; list($ret, $err) = $bucketMgr->stat($bucket, $key); echo "\n====> stat result: \n"; if ($err !== null) { var_dump($err); } else { var_dump($ret); } if (!empty($_FILES['headpic'])) { $user = User::findByUsername(Yii::$app->user->identity->username); $image = UploadedFile::getInstanceByName('headpic'); $imageName = $user->id . '_' . time() . '.' . $image->getExtension(); if ($image->saveAs('uploads/head/' . $imageName)) { // file is uploaded successfully echo 'yes'; die; } else { echo 'no'; die; } } else { return $this->render('headupload', ['title' => '个人资料', 'category' => '账号管理', 'subcate' => '个人资料']); } }
/** * 删除图片 * $param string $fileName 图片名称 * return null */ public function deleteFile($fileName) { $bucketMgr = new BucketManager($this->auth); $err = $bucketMgr->delete($this->bucket, $fileName); if ($err !== null) { return $err; } else { return "Success!"; } }
static function list_file($bucketName, $path = null) { if ($path && substr($path, 0, 1) == "/") { $path = substr($path, 1); } //echo $path;exit; //$path = null; $auth = self::get_auth(); $bucketManager = new BucketManager($auth); list($items, $marker, $error) = $bucketManager->listFiles($bucketName, $path, null, 2); return $items; }
public function fire($job, $keys) { $qiniu_config = Config::get('qiniu'); $auth = new Auth($qiniu_config['accessKey'], $qiniu_config['secretKey']); $bucket_manager = new BucketManager($auth); foreach ($keys as $key) { $response = $bucket_manager->delete($qiniu_config['buckets'][0], $key); if ($response) { Log::info("Error in delete key:{$key}" . $response->message()); } } }
/** * 上传头像 * 图片上传成功后做标记,渲染裁剪功能,裁剪后坐标传送给Headcut,Headcut进行裁剪,保存图片,标记。 */ public function actionIndex() { //如果用户上传了头像图片 if (!empty($_FILES['headpic'])) { //进行本地上传 $user = User::find()->where(['id' => $_GET['id']])->one(); $image = UploadedFile::getInstanceByName('headpic'); $imageName = $user->id . '_' . time() . '.' . $image->getExtension(); if ($image->saveAs('uploads/head/' . $imageName)) { //上传到本地成功,进行图片处理 //组装文件名 $imagePath = Yii::getAlias('@root') . '\\frontend\\web\\uploads\\head\\' . $imageName; // 处理成功后 传入七牛 //配置七牛的参数 $accessKey = Yii::getAlias('@accessKey'); $secretKey = Yii::getAlias('@secretKey'); $auth = new Auth($accessKey, $secretKey); $bucket = 'colfans-uploads-public'; $token = $auth->uploadToken($bucket); $uploadMgr = new UploadManager(); list($ret, $err) = $uploadMgr->putFile($token, $imageName, $imagePath); if ($err !== null) { //上传不成功 echo 'no'; } else { //上传成功 //删除本地文件 unlink($imagePath); //更新数据库头像 $oldHeadFile = $user->head; $user->head = $imageName; if ($user->save()) { //删除七牛原有的头像图片 $bucketMgr = new BucketManager($auth); $err = $bucketMgr->delete($bucket, $oldHeadFile); if ($err !== null) { echo 'no'; } else { echo 'yes'; } } } } else { } } else { return $this->render('headupload', ['title' => '个人资料', 'category' => '账号管理', 'subcate' => '个人资料']); } }
public function deleteImageByQiniuKey($key) { vendor("qiniusdk.autoload"); $accessKey = 'k7HBysPt-HoUz4dwPT6SZpjyiuTdgmiWQE-7qkJ4'; $secretKey = 'BuaBzxTxNsNUBSy1ZvFUAfUbj8GommyWbfJ0eQ2R'; //初始化Auth状态 $auth = new Auth($accessKey, $secretKey); //初始化BucketManager $bucketMgr = new BucketManager($auth); $bucket = 'image'; if ($key == "") { return false; } else { $err = $bucketMgr->delete($bucket, $key); if ($err !== null) { return false; } else { return true; } } }
public function fetchImage($url, $imageName) { $bucketMgr = new BucketManager($this->auth); return $bucketMgr->fetch($url, $this->bucket, $imageName); }
/** * {@inheritdoc} */ public function getMimetype($path) { $r = $this->bucketManager->stat($this->bucket, $path); return ['mimetype' => $r[0]['mimeType']]; }
/** * @inheritdoc */ public function deleteDir($dirname) { // 七牛无目录概念. 目前实现方案是.列举指定目录资源.批量删除 $keys = array_map(function ($file) { return $file['key']; }, $this->listDirContents($dirname)); list(, $err) = $this->getBucketManager()->batch(BucketManager::buildBatchDelete($keys)); return $err === null; }
public function listFiles() { $res = array("code" => 0, "msg" => ''); $bucketMgr = new BucketManager(self::auth()); $bucket = 'blog'; $prefix = ''; $marker = ''; $limit = ''; list($items, $marker, $err) = $bucketMgr->listFiles($bucket, $prefix, $marker, $limit); if ($err !== null) { $res["code"] = 1; $res["msg"] = $err; } else { $time = time() + 7 * 24 * 60 * 60; foreach ($items as $item) { $url = self::QI_NIU_DOMAIN . $item["key"] . "?e={$time}"; $sign = hash_hmac('sha1', $url, self::getKey("secret"), true); $token = self::getKey("access") . ":" . self::urlsafe_base64_encode($sign); $item["url"] = $url . "&token={$token}"; $item["url"] = self::QI_NIU_DOMAIN_BLOG . $item["key"]; $newItems[] = $item; } $res["data"] = $newItems; } echo json_encode($res); }
<?php require_once __DIR__ . '/../autoload.php'; use Qiniu\Auth; use Qiniu\Storage\BucketManager; $accessKey = 'Access_Key'; $secretKey = 'Secret_Key'; //初始化Auth状态: $auth = new Auth($accessKey, $secretKey); //初始化BucketManager $bucketMgr = new BucketManager($auth); //你要测试的空间, 并且这个key在你空间中存在 $bucket = 'Bucket_Name'; $key = 'php-logo.png'; //将文件从文件$key 改成文件名$key2。 可以在不同bucket移动 $key3 = 'php-logo3.png'; $err = $bucketMgr->move($bucket, $key2, $bucket, $key3); echo "\n====> move {$key} to {$key2} : \n"; if ($err !== null) { var_dump($err); } else { echo "Success!"; }
<?php require_once __DIR__ . '/../autoload.php'; use Qiniu\Auth; use Qiniu\Storage\BucketManager; $accessKey = 'Access_Key'; $secretKey = 'Secret_Key'; $auth = new Auth($accessKey, $secretKey); $bmgr = new BucketManager($auth); $url = 'http://php.net/favicon.ico'; $bucket = 'Bucket_Name'; $key = time() . '.ico'; list($ret, $err) = $bmgr->fetch($url, $bucket, $key); echo "=====> fetch {$url} to bucket: {$bucket} key: {$key}\n"; if ($err !== null) { var_dump($err); } else { echo 'Success'; }
/** * 推送图片 **/ public function actionpushimage() { $res = array("status" => 0, "mes" => "", "data" => array(), "image_url" => ""); $brandid = isset($_SESSION['brandid']) ? $_SESSION['brandid'] : 0; //品牌ID $userid = $_SESSION['user_id']; //用户ID $push_detaile = Yii::app()->request->getParam("push_detaile"); //推送信息 $cut_detaile = Yii::app()->request->getParam("cut_detaile"); //裁剪信息 $imageid = Yii::app()->request->getParam("imageid"); //图片ID $skc = Yii::app()->request->getParam("skc"); //衣服skc try { //通过图片ID查找图片信息 $image_obj = Yii::app()->db->createCommand()->select('url,name')->from('erp_image')->where("id={$imageid}")->queryRow(); $image_url = ""; $image_name = ""; if (!empty($image_obj)) { $image_url = $image_obj['url']; //衣服图片原图 $res['image_url'] = $image_obj['url']; //衣服名 } else { throw new Exception('图片信息无法获取'); } $pushdetaile_class = new pushDetaileclass(); $array = array("watermark_id" => $push_detaile['watermarkid'], "watermark_width" => $cut_detaile['watermarkwidth'], "watermark_height" => $cut_detaile['watermarkheight'], "watermark_url" => $push_detaile['watermarkurl'], "watermark_positionx" => $cut_detaile['positionx'], "watermark_positiony" => $cut_detaile['positiony'], "patform_width" => $cut_detaile['width'], "patform_height" => $cut_detaile['height'], "image_url" => $image_url, "cut_positionx" => $push_detaile['positionx'], "cut_positiony" => $push_detaile['positiony'], "cut_width" => $push_detaile['CutOut_width'], "cut_height" => $push_detaile['CutOut_height'], "img_w" => $push_detaile['img_w'], "img_h" => $push_detaile['img_h']); $end_obj = $pushdetaile_class->cut_image($array, $brandid); //print_r($end_obj); if ($end_obj['status'] == 0) { throw new Exception('图片生成失败'); } $qiniu_image_url = $end_obj['url']; $qiniu_array = $end_obj['qiniu_array']; //裁剪完成的数据 if (count($qiniu_array) == 0) { throw new Exception('七牛账户信息有误'); } //access_key、secret_key、space、domain $status = 1; //1为失败,0为成功 $content = @file_get_contents($qiniu_image_url); if ($content) { $imagename = md5($qiniu_image_url); $image = $content; $ProcessImagepath = $_SERVER['DOCUMENT_ROOT'] . "/uploads/image/" . $imagename . ".jpg"; $fp = fopen($ProcessImagepath, 'w'); fwrite($fp, $image); //写入文件 fclose($fp); $image_array = explode("/", $image_url); $file = ""; $image_count = count($image_array); for ($i = 0; $i < $image_count; $i++) { if ($i == $image_count - 1) { $file .= $imagename . ".jpg"; } else { $file .= $image_array[$i] . "/"; } } $key3 = $file; $bucket = $qiniu_array[0]['space']; $auth = new Auth($qiniu_array[0]['access_key'], $qiniu_array[0]['secret_key']); $bucketMgr = new BucketManager($auth); //删除$bucket 中的文件 $key $err = $bucketMgr->delete($bucket, $key3); $uploadfile_ret = $this->qinuu_upload($file, $ProcessImagepath); @unlink($ProcessImagepath); if (!isset($uploadfile_ret[0]['hash'])) { throw new Exception('图片上传失败'); } /*******推送到平台***start***/ /*******推送到平台***end***/ $status = 0; //修改推送数据 $pushdetaile_class->update_push($push_detaile['id'], array('pushstatus' => 1, 'cut_src' => $file)); $res['status'] = 1; } $pushcount_array = array('skc' => $push_detaile['skc'], 'sku' => $push_detaile['sku'], 'addtime' => date("Y-m-d H:i:s"), 'patformid' => $cut_detaile['patformid'], 'status' => $status, 'clothesimageid' => $imageid, 'userid' => $userid, 'brandid' => $brandid); $pushcount_class = new pushcountclass(); $pushcount_class->insert_push_count($pushcount_array); //插入信息 $res['id'] = $push_detaile['imageid']; } catch (Exception $e) { $ret['msg'] = $e->getMessage(); } echo json_encode($res); exit; }
} } } $ak = $_SESSION['ak']; $sk = $_SESSION['sk']; $bucket = $_SESSION['bucket']; $domain = $_SESSION['domain']; $mode = $_SESSION['mode']; $width = $_SESSION['width']; $height = $_SESSION['height']; $sn = $_SESSION['sn']; console_log("Access Info: {$ak}, {$sk}, {$bucket}"); console_log("Op Info: {$mode}, {$width}, {$height}"); console_log("Selected File: {$sn}"); $auth = new Auth($ak, $sk); $bm = new BucketManager($auth); list($items, $marker, $err) = $bm->listFiles($bucket); // 尝试列举空间中的文件 if ($err != null) { //echo "列举文件失败:(".$err->code().") ".$err->message(); } else { // 过滤出若干张jpg图片用于展示。 foreach ($items as $item) { if ($item['mimeType'] == 'image/jpeg') { $pics[] = $item; if (count($pics) >= 10) { break; } } } }
public function testBatchStat() { $ops = BucketManager::buildBatchStat($this->bucketName, array('php-sdk.html')); list($ret, $error) = $this->bucketManager->batch($ops); $this->assertEquals(200, $ret[0]['code']); }
<?php require_once __DIR__ . '/../autoload.php'; use Qiniu\Auth; use Qiniu\Storage\BucketManager; $accessKey = 'Access_Key'; $secretKey = 'Secret_Key'; //初始化Auth状态: $auth = new Auth($accessKey, $secretKey); //初始化BucketManager $bucketMgr = new BucketManager($auth); //你要测试的空间, 并且这个key在你空间中存在 $bucket = 'Bucket_Name'; $key = 'php-logo.png'; //删除$bucket 中的文件 $key $err = $bucketMgr->delete($bucket, $key); echo "\n====> delete {$key} : \n"; if ($err !== null) { var_dump($err); } else { echo "Success!"; }
<?php require_once __DIR__ . '/../autoload.php'; use Qiniu\Auth; use Qiniu\Storage\BucketManager; $accessKey = 'Access_Key'; $secretKey = 'Secret_Key'; //初始化Auth状态: $auth = new Auth($accessKey, $secretKey); //初始化BucketManager $bucketMgr = new BucketManager($auth); //你要测试的空间, 并且这个key在你空间中存在 $bucket = 'Bucket_Name'; $key = 'php-logo.png'; //将文件从文件$key 复制到文件$key2。 可以在不同bucket复制 $key2 = 'php-logo2.png'; $err = $bucketMgr->copy($bucket, $key, $bucket, $key2); echo "\n====> copy {$key} to {$key2} : \n"; if ($err !== null) { var_dump($err); } else { echo "Success!"; }
/** * 重命名 * @param $oldFileName * @param $newFileName * @return mixed */ public function rename($oldFileName, $newFileName) { $client = new BucketManager($this->auth); return $client->rename($this->bucket, $oldFileName, $newFileName); }
protected function _del_hc51($key) { $qiniu_config = Kohana::config('qiniu.haoche51'); $accessKey = $qiniu_config['access_key']; $secretKey = $qiniu_config['secret_key']; $bucket = 'haoche51post'; $auth = new Auth($accessKey, $secretKey); $bucketMgr = new BucketManager($auth); $err = $bucketMgr->delete($bucket, $key); if ($err === null) { return true; } else { return false; } }
<?php require_once __DIR__ . '/../autoload.php'; use Qiniu\Auth; use Qiniu\Storage\BucketManager; $accessKey = 'Access_Key'; $secretKey = 'Secret_Key'; $auth = new Auth($accessKey, $secretKey); $bucketMgr = new BucketManager($auth); $bucket = 'Bucket_Name'; $prefix = ''; $marker = ''; $limit = 3; list($iterms, $marker, $err) = $bucketMgr->listFiles($bucket, $prefix, $marker, $limit); if ($err !== null) { echo "\n====> list file err: \n"; var_dump($err); } else { echo "Marker: {$marker}\n"; echo "\nList Iterms====>\n"; var_dump($iterms); }
public function Delete($filename) { $Bucket = new BucketManager($this->auth); $err = $Bucket->delete($this->bucket, $filename); return $err !== null ? '' : true; }
<?php require_once __DIR__ . '/../autoload.php'; use Qiniu\Auth; use Qiniu\Storage\BucketManager; $accessKey = 'Access_Key'; $secretKey = 'Secret_Key'; //初始化Auth状态: $auth = new Auth($accessKey, $secretKey); //初始化BucketManager $bucketMgr = new BucketManager($auth); //你要测试的空间, 并且这个key在你空间中存在 $bucket = 'Bucket_Name'; $key = 'php-logo.png'; //获取文件的状态信息 list($ret, $err) = $bucketMgr->stat($bucket, $key); echo "\n====> {$key} stat : \n"; if ($err !== null) { var_dump($err); } else { var_dump($ret); }
/** * Deletes a particular model. * If deletion is successful, the browser will be redirected to the 'admin' page. * @param integer $id the ID of the model to be deleted */ public function actionDelete($id) { $model = $this->loadModel($id); $accessKey = 'alO3SWK6jx4XvPSeUCbhRRb8arS8sivFSQ_zMo1K'; $secretKey = 'qZlz5D8-Wlj0nUnsRt-qTNit56dVcWb2jK_eVqcK'; //初始化Auth状态: $auth = new Auth($accessKey, $secretKey); //初始化BucketManager $bucketMgr = new BucketManager($auth); //你要测试的空间, 并且这个key在你空间中存在 $bucket = 's-nice'; $key = $model->img; //删除$bucket 中的文件 $key $err = $bucketMgr->delete($bucket, $key); if (file_exists(Yii::app()->params['uploadPath'] . $model->img)) { unlink(Yii::app()->params['uploadPath'] . $model->img); } $model->delete(); // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser if (!isset($_GET['ajax'])) { $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); } }
<?php require_once '../autoload.php'; use Qiniu\Auth; use Qiniu\Storage\BucketManager; $accessKey = 'Access_Key'; $secretKey = 'Secret_Key'; //初始化Auth状态: $auth = new Auth($accessKey, $secretKey); //初始化BucketManager $bucketMgr = new BucketManager($auth); //你要测试的空间, 并且这个key在你空间中存在 $bucket = 'Bucket_Name'; $key = 'php-logo.png'; //获取文件的状态信息 list($ret, $err) = $bucketMgr->stat($bucket, $key); echo "\n====> {$key} stat : \n"; if ($err !== null) { var_dump($err); } else { var_dump($ret); } //将文件从文件$key 复制到文件$key2。 可以在不同bucket复制 $key2 = 'php-logo2.png'; $err = $bucketMgr->copy($bucket, $key, $bucket, $key2); echo "\n====> copy {$key} to {$key2} : \n"; if ($err !== null) { var_dump($err); } else { echo "Success!"; }
/** * 查看文件状态 * * @param $fileName * @return */ public function checkStatus($fileName) { $bucketManager = new BucketManager($this->auth); list($result, $error) = $bucketManager->stat($this->bucket, $fileName); if (empty($result)) { return $error->getResponse()->error; } else { return $result; } }