Summary of the experience of using Qiniu cloud object storage

A. Using the Platform

OS: windows 7 x64
Server: Apache24 2.4.25
PHP: PHP-7.1.17
MySQL: MySQL-5.7.x

B. Download the zip source package

clipboard.png

Specific document address: https://developer.qiniu.com/k…

C. Know some unique terms

  1. space ( bucket)

clipboard.png

  1. public key ( AccessKey)
  2. private key ( SecretKey)

clipboard.png

D. Upload files

/* 七牛云文件上传 demo */
require_once 'qiniu-sdk\autoload.php';

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

$file   = 'd:/Website/FrontToolLib/images/02.jpg';
$bucket = 'grayvtouch';
$ak     = '5MrBnlymEEC4rID_la3rhZhu6R-UBI6wAkdXVmU_';
$sk     = 'b0BzIAijiKio2mQLCn-LAAAIgiHp9Yog4lmgnv17';

$auth  = new Auth($ak , $sk);
$token = $auth->uploadToken($bucket);

$upload_mgr = new UploadManager();
$rel        = $upload_mgr->putFile($token , '03.jpg' , $file);

print_r($rel);

E. Online viewing of documents

Online preview of private space (requires expiration time + token):

/* 七牛云文件下载 demo */
require 'qiniu-sdk/autoload.php';

// 引入鉴权类
use Qiniu\Auth;

// 需要填写你的 Access Key 和 Secret Key
$bucket = 'grayvtouch';
$ak = '5MrBnlymEEC4rID_la3rhZhu6R-UBI6wAkdXVmU_';
$sk = 'b0BzIAijiKio2mQLCn-LAAAIgiHp9Yog4lmgnv17';
$key = '01.jpg';
$style = 'new';
$domain = 'ol6vrkdg4.bkt.clouddn.com';

// 构建鉴权对象
$auth = new Auth($ak, $sk);

//baseUrl构造成私有空间的域名/key的形式
// 原图保护已开启(到七牛云控制面板查看样式分隔符 和 样式名称)
$baseUrl = $domain . '/' . $key . '-' . $style;
// 原图保护未开启
$baseUrl = $domain . '/' . $key;
$authUrl = $auth->privateDownloadUrl($baseUrl);

echo $authUrl;

Online preview of public space images:

// 未开启原图保护
$domain . '/' . $filename;
// 已开启原图保护
$domain . '/' . $filename . '-' . $style;

F. Space management: get file information, rename files, delete files, move files

// 载入七牛SDK
require_once './QiNiu_SDK/autoload.php';

$accessKey = '5MrBnlymEEC4rID_la3rhZhu6R-UBI6wAkdXVmU_';
$secretKey = 'b0BzIAijiKio2mQLCn-LAAAIgiHp9Yog4lmgnv17';
$bucket       = 'grayvtouch';
$domain    = 'ol6vrkdg4.bkt.clouddn.com';

use Qiniu\Auth;
use Qiniu\Storage\BucketManager;

$auth  = new Auth($accessKey , $secretKey);
$bk    = new BucketManager($auth);

// 获取文件信息
$f_info = $bk->stat($bucket , $fname);

// 重命名文件
$new_name = 'test.jpg';
$rename = $bk->rename($bucket , $fname , $new_name);

// 移动文件(移动到不同的空间里面)
$other_bucket = 'uploadpic';
$bk->move($other_bucket , $new_name , $other_bucket , $new_name);

// 删除文件
$bk->delete($other_bucket , $new_name);

G. Get the file name after generating the Qiniu cloud access link

// 获取七牛云文件访问链接中的文件名(只针对公开的空间!)
function get_qn_fname($link= ''){
    $s_idx = mb_strrpos($link , '/') + 1;
    $e_idx = mb_strrpos($link , '?');
    
    // mb_string 为 base.php 中函数!
    // 需自行实现
    $fname = mb_substring($src , $s_idx , $e_idx);
    
    if (strpos($fname , '-') === false) {
        // 未设置原图保护
        return $fname;
    }
    
    // 已设置原图保护
    $data = explode('-' , $fname);

    return $data[0];
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324774450&siteId=291194637