实际项目中受限于服务器容量及带宽影响,上传资源很多时候需要用到第三方对象存储服务,国内比较主流的服务提供商有七牛云、阿里等。本文以七牛云为实例,来讲解如何将用户的视频上传放置到七牛云等服务商的空间中。
七牛云上传主要有两种:
服务端上传
前端上传,前端又分两种返回方式:
1).重定向返回,可以解决ajax跨域的问题
2).回调返回,七牛云先向服务端要返回数据,再由七牛云返回前端,解决不支持重定向的请求方式,比如小程序上传
本次使用的是 七牛云 php sdk;
composer require qiniu/php-sdk
本次使用了kindeditor编辑器。
在kindeditor/php 下添加 config.php 主要是配置参数
<?php
error_reporting(0);
defined('root_path') || define('root_path', dirname(__dir__).'/');
defined('qiniu_access_key') || define('qiniu_access_key', '');
defined('qiniu_secret_key') || define('qiniu_secret_key', '');
defined('qiniu_test_bucket') || define('qiniu_test_bucket', '七牛云空间名');
defined('qiniu_bucket_domain') || define('qiniu_bucket_domain', '七牛云空间网址');
defined('callback_url') || define('callback_url', '域名/kindeditor/php/callback.php');
defined('return_url') || define('return_url', '域名/kindeditor/php/returnback.php');
require_once root_path."vendor/autoload.php";
在kindeditor/php 下添加 qiniu_token.php 主要是生成上传用的 token
<?php
use qiniu\auth;
require_once __dir__."/config.php";
// 构建鉴权对象
$auth = new auth(qiniu_access_key, qiniu_secret_key);
$data = [
'returnurl' => return_url,
];
if (isset($_request['is_call'])) {
$data = [
'callbackurl' => callback_url,
'callbackbody' => 'key=$(key)&hash=$(etag)&w=$(imageinfo.width)&h=$(imageinfo.height)'
];
}
// 生成上传 token
$token = $auth->uploadtoken(qiniu_test_bucket, null, 3600, $data);
echo json_encode([
'error' => 0,
'token' => $token
]);
在kindeditor/php 下添加 callback.php 主要是回调用
<?php
use qiniu\auth;
require_once __dir__."/config.php";
$_body = file_get_contents('php://input');
$auth = new auth(qiniu_access_key, qiniu_secret_key);
//回调的contenttype
$contenttype = 'application/x-www-form-urlencoded';
//回调的签名信息,可以验证该回调是否来自七牛
$authorization = $_server['http_authorization'];
$isqiniucallback = $auth->verifycallback($contenttype, $authorization, callback_url, $_body);
if (!$isqiniucallback) {
echo json_encode([
'error' => 2,
'message' => '验证失败'
]);
die();
}
$body = $_post;
$qiniu_url = qiniu_bucket_domain;
if (!empty($body['key'])) {
echo json_encode([
'error' => 0,
'url' => $qiniu_url.$body['key']
]);
die();
}
echo json_encode([
'error' => 1,
'message' => '视频上传出错'
]);
在kindeditor/php 下添加 returnback.php 主要是重定向接收地址
<?php
use qiniu\auth;
require_once __dir__."/config.php";
$upload_ret = base64_decode($_get['upload_ret']);
$upload_ret = json_decode($upload_ret, true);
$qiniu_url = qiniu_bucket_domain;
if (!empty($upload_ret['key'])) {
echo json_encode([
'error' => 0,
'url' => $qiniu_url.$upload_ret['key']
]);
die();
}
echo json_encode([
'error' => 1,
'message' => '视频上传出错'
]);
接下来是前端更改,我改的是视频上传
kindeditor/plugins/media/media.js
kindeditor.plugin('media', function(k) {
var self = this, name = 'media', lang = self.lang(name + '.'),
allowmediaupload = k.undef(self.allowmediaupload, true),
allowfilemanager = k.undef(self.allowfilemanager, false),
formatuploadurl = k.undef(self.formatuploadurl, true),
extraparams = k.undef(self.extrafileuploadparams, {
'token': ''//添加token
}),
filepostname = k.undef(self.filepostname, 'file'), //更改文件上传名
uploadjson = k.undef(self.uploadjson, 'https://up.qbox.me'); //更改上传地址,我用的时华东区的空间使用https
....
function getqtoken() {
$.getjson('/includes/kindeditor/php/qiniu_token.php', function (data) {
k('[name="token"]', div).val(data.token);
});
}
// 获取设置上传token
getqtoken();
if (allowmediaupload) {
var uploadbutton = k.uploadbutton({
button : k('.ke-upload-button', div)[0],
fieldname : filepostname,
extraparams : extraparams,
url : uploadjson,//去除添加参数
afterupload : function(data) {
...
});
这样就可以上传视频到七牛云了。
相关推荐:
微信小程序如何实现对接七牛云存储的实例
laravel中图片使用七牛云上传的实例介绍
yii2.0 oss的示例代码
以上就是php应用七牛云的重定向上传和回调上传实例分享的详细内容。