1、SDK安装
github地址:https://github.com/aliyun/aliyun-oss-php-sdk
2、复制aliyun-oss-php-sdk-master/src/OSS至passport.hmwis.com/ThinkPHP/Library/Vendor/OSS,如图1、2
3、注册新的命名空间OSS,编辑文件passport.hmwis.com/Application/Common/Conf/config.php,如图3
‘AUTOLOAD_NAMESPACE’ => array(‘Addons’ => ONETHINK_ADDON_PATH, ‘OSS’ => VENDOR_PATH.’OSS’), //扩展模块列表
4、实例化OSS下面的类库时,如
new OSS/OssClient();
自动加载对应的类库文件OssClient.class.php,因此需要重命名OSS目录下的文件名,将OssClient.php重命名为OssClient.class.php,OSS目录下所有文件皆如是处理,如图4
5、编辑passport.hmwis.com/Application/Common/Conf/config.php,生产环境下的OSS数据中心访问域名设置为内网,如图5
/* OSS初始化设置 */
‘OSS’ => array(
‘ACCESS_KEY_ID’ => ”, //从OSS获得的AccessKeyId
‘ACCESS_KEY_SECRET’ => ”, //从OSS获得的AccessKeySecret
‘ENDPOINT’ => ‘oss-cn-hangzhou-internal.aliyuncs.com’, //您选定的OSS数据中心访问域名,例如oss-cn-hangzhou.aliyuncs.com
),
6、编辑passport.hmwis.com/Application/Common/Conf/debug.php,开发环境下的OSS数据中心访问域名设置为外网,如图6
/* OSS初始化设置 */
‘OSS’ => array(
‘ACCESS_KEY_ID’ => ”, //从OSS获得的AccessKeyId
‘ACCESS_KEY_SECRET’ => ”, //从OSS获得的AccessKeySecret
‘ENDPOINT’ => ‘oss-cn-hangzhou.aliyuncs.com’, //您选定的OSS数据中心访问域名,例如oss-cn-hangzhou.aliyuncs.com
),
7、实现上传本地文件至OSS的功能,如修改头像,如图7:
8、编辑passport.hmwis.com/Application/Home/Controller/ProfileController.class.php,导入OSS的客户端类、异常类,如图8
use OSS/OssClient;
use OSS/Core/OssException;
9、实例化OssClient类,调用上传本地文件方法,如图9
/**
* 根据Config配置,得到一个OssClient实例
*/
try {
$ossClient = new OssClient(C(‘OSS.ACCESS_KEY_ID’), C(‘OSS.ACCESS_KEY_SECRET’), C(‘OSS.ENDPOINT’), false);
} catch (OssException $e) {
$return[‘status’] = 0;
$return[‘info’] = $e->getMessage();
/* 返回JSON数据 */
$this->ajaxReturn($return);
}
$bucket = ‘avatar-ygt-cm’;
try {
$ossClient->uploadFile($bucket,$oss_file_path,$avatar);
} catch (OssException $e) {
$return[‘status’] = 0;
$return[‘info’] = $e->getMessage();
/* 返回JSON数据 */
$this->ajaxReturn($return);
}
10、基于ThinkPHP文件上传操作使用Think/Upload类定制,实现通过表单上传文件至OSS,复制passport.hmwis.com/ThinkPHP/Library/Think/Upload.class.php至passport.hmwis.com/ThinkPHP/Library/Vendor/OSS/OssUpload.class.php,编辑passport.hmwis.com/ThinkPHP/Library/Vendor/OSS/OssUpload.class.php,如图10
namespace OSS;
use OSS/OssClient;
use OSS/Core/OssException;
11、编辑默认上传配置,如图11
/**
* 默认上传配置
* @var array
*/
private $config = array(
‘accessKeyId’ => ‘l0k4ZMxs44FAjAWH’, //AccessKeyId
‘accessKeySecret’ => ‘hjU4DPhzKl6pwT6K0HlTkVMNaE9rZo’, //AccessKeySecret
‘endpoint’ => ‘oss-cn-hangzhou.aliyuncs.com’, //区域地址
‘mimes’ => array(), //允许上传的文件MiMe类型
‘maxSize’ => 0, //上传的文件大小限制 (0-不做限制)
‘exts’ => array(), //允许上传的文件后缀
‘autoSub’ => true, //自动子目录保存文件
‘subName’ => array(‘date’, ‘Y-m-d’), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
‘rootPath’ => ‘./Uploads/’, //保存根路径
‘savePath’ => ”, //保存路径
‘saveName’ => array(‘uniqid’, ”), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组
‘saveExt’ => ”, //文件保存后缀,空则使用原后缀
‘replace’ => false, //存在同名是否覆盖
‘hash’ => true, //是否生成hash编码
‘callback’ => false, //检测文件是否存在回调,如果存在返回文件信息数组
);
注:主要为取消文件上传驱动及文件目录相关的操作功能实现,由于改动过多,建议查看SVN日志,或者直接复制OssUpload.class.php;
12、编辑上传文件方法upload,如图12
/* 保存文件 并记录保存成功的文件 */
/*
if ($this->uploader->save($file,$this->replace)) {
unset($file[‘error’], $file[‘tmp_name’]);
$info[$key] = $file;
} else {
$this->error = $this->uploader->getError();
}*/
/**
* 根据Config配置,得到一个OssClient实例
*/
try {
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint, false);
} catch (OssException $e) {
$this->error = $e->getMessage();
}
/**
* 上传本地文件
*/
$bucket = ‘avatar-ygt-cm’;
$object = $this->rootPath . $file[‘savepath’] . $file[‘savename’];
try {
$ossClient->uploadFile($bucket,$object,$file[‘tmp_name’]);
unset($file[‘error’], $file[‘tmp_name’]);
$file[‘oss’] = $bucket . ‘/’ . $object;
$info[$key] = $file;
} catch (OssException $e) {
$this->error = $e->getMessage();
}
13、实现通过表单上传文件至OSS,编辑passport.hmwis.com/Application/Home/Controller/ProfileController.class.php,导入OSS的文件上传类
use OSS/OssUpload;
所有上传实现建议参考Think/Upload类的文档,基本上一致
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/180183.html