[更新]完善AliOSS支持

This commit is contained in:
Anyon 2017-05-11 11:52:03 +08:00
parent 573b2f4946
commit 81ec32a31d
3 changed files with 51 additions and 15 deletions

View File

@ -15,7 +15,9 @@
namespace app\admin\controller;
use controller\BasicAdmin;
use OSS\OssClient;
use service\FileService;
use think\View;
/**
* 插件助手控制器
@ -40,7 +42,7 @@ class Plugs extends BasicAdmin {
/**
* 文件上传
* @return \think\response\View
* @return View
*/
public function upfile() {
$types = $this->request->get('type', 'jpg,png');
@ -94,6 +96,14 @@ class Plugs extends BasicAdmin {
case 'local':
$config['server'] = FileService::getUploadLocalUrl();
break;
case 'oss':
$time = time() + 3600;
$policyText = ['expiration' => date('Y-m-d', $time) . 'T' . date('H:i:s') . '.000Z', 'conditions' => [['content-length-range', 0, 1048576000]]];
$config['policy'] = base64_encode(json_encode($policyText));
$config['server'] = FileService::getUploadOssUrl();
$config['site_url'] = FileService::getBaseUriOss() . $filename;
$config['signature'] = base64_encode(hash_hmac('sha1', $config['policy'], sysconf('storage_oss_secret'), true));
$config['OSSAccessKeyId'] = sysconf('storage_oss_keyid');
}
$this->result($config, 'NOT_FOUND');
}

View File

@ -196,8 +196,13 @@
console.log('文件秒传成功 --> ' + file.name);
} else {
file.md5 = md5;
file.token = ret.data.token;
file.key = ret.data.file_url;
file.token = ret.data.token || '';
file.key = ret.data.file_url || '';
file.site_url = ret.data.site_url || '';
me.options.formData.OSSAccessKeyId = ret.data.OSSAccessKeyId || ''; // OSS
me.options.formData.signature = ret.data.signature; // OSS
me.options.formData.policy = ret.data.policy; // OSS
me.options.formData.success_action_status = '200'; // OSS
me.options.server = ret.data.server;
}
deferred.resolve();
@ -240,16 +245,31 @@
fileSingleSizeLimit: 200 * 1024 * 1024 // 200 M
});
/* 上传开始前的处理 */
uploader.on('uploadBeforeSend', function (file, data, header) {
header['X_Requested_With'] = 'XMLHttpRequest';
data['allowed_types'] = this.options.accept[0].extensions.split(',').join('|');
data['md5'] = file.file.md5;
data['key'] = file.file.key;
data['token'] = file.file.token;
});
/**
* 处理上传后的结果
* @param {type} file
* @returns {undefined}
*/
uploader.on('uploadAccept', function (fieldata, ret) {
// Qiniu or Local 上传
if (ret.code === 'SUCCESS') {
uploaded.call(uploader, ret.data, fieldata.file);
return true;
}
// AliOSS 上传
if (fieldata.file.site_url) {
uploaded.call(uploader, {'site_url': fieldata.file.site_url}, fieldata.file);
return true;
}
return false;
});
@ -266,14 +286,7 @@
return !denied;
});
/* 上传开始前的处理 */
uploader.on('uploadBeforeSend', function (file, data, header) {
header['X_Requested_With'] = 'XMLHttpRequest';
data['allowed_types'] = this.options.accept[0].extensions.split(',').join('|');
data['md5'] = file.file.md5;
data['key'] = file.file.key;
data['token'] = file.file.token;
});
// 添加“添加文件”的按钮,
uploader.addButton({
@ -484,7 +497,6 @@
if (stats.successNum) {
completed.call(this);
} else {
// 没有成功的文件,重设
state = 'done';
location.reload();
}
@ -515,9 +527,7 @@
uploader.onfieldequeued = function (file) {
fileCount--;
fileSize -= file.size;
if (!fileCount) {
setState('pedding');
}
!fileCount && setState('pedding');
removeFile(file);
updateTotalProgress();
};

View File

@ -64,6 +64,8 @@ class FileService {
return self::getBaseUriLocal() . $filename;
case 'qiniu':
return self::getBaseUriQiniu() . $filename;
case 'oss':
return self::getBaseUriOss() . $filename;
}
return false;
}
@ -109,6 +111,14 @@ class FileService {
}
}
/**
* 获取AliOSS上传地址
* @return type
*/
public static function getUploadOssUrl() {
return (request()->isSsl() ? 'https' : 'http') . '://' . sysconf('storage_oss_domain');
}
/**
* 获取服务器URL前缀
* @return string
@ -154,6 +164,9 @@ class FileService {
$bucketMgr = new BucketManager($auth);
list($ret, $err) = $bucketMgr->stat(sysconf('storage_qiniu_bucket'), $filename);
return $err === null;
case 'oss':
$ossClient = new OssClient(sysconf('storage_oss_keyid'), sysconf('storage_oss_secret'), self::getBaseUriOss(), true);
return $ossClient->doesObjectExist(sysconf('storage_oss_bucket'), $filename);
}
return false;
}
@ -174,6 +187,9 @@ class FileService {
case 'qiniu':
$auth = new Auth(sysconf('storage_qiniu_access_key'), sysconf('storage_qiniu_secret_key'));
return file_get_contents($auth->privateDownloadUrl(self::getBaseUriQiniu() . $filename));
case 'oss':
$ossClient = new OssClient(sysconf('storage_oss_keyid'), sysconf('storage_oss_secret'), self::getBaseUriOss(), true);
return $ossClient->getObject(sysconf('storage_oss_bucket'), $filename);
}
Log::error("通过{$storage}读取文件{$filename}的不存在!");
return null;