没有不值得去解决的问题,也没有不值得去学习的技术!

Integrate Alibaba Cloud OSS PHP-SDK 2.0.4 Into OneThink (ThinkPHP3.2.3) for Local File Uploads

Edit the upload file method upload

1. SDK installation

GitHub URL: https://github.com/aliyun/aliyun-oss-php-sdk

2. Copy aliyun-oss-php-sdk-master\src\OSS to passport.hmwis.com\ThinkPHP\Library\Vendor\OSS, as shown in Figures 1 and 2

Copy aliyun-oss-php-sdk-master\src\OSS to passport.hmwis.com\ThinkPHP\Library\Vendor\OSS
Copy aliyun-oss-php-sdk-master\src\OSS to passport.hmwis.com\ThinkPHP\Library\Vendor\OSS
Copy aliyun-oss-php-sdk-master\src\OSS to passport.hmwis.com\ThinkPHP\Library\Vendor\OSS
Copy aliyun-oss-php-sdk-master\src\OSS to passport.hmwis.com\ThinkPHP\Library\Vendor\OSS

3. Register the new namespace OSS by editing the file passport.hmwis.com\Application\Common\Conf\config.php, as shown in Figure 3

‘AUTOLOAD_NAMESPACE’ => array(‘Addons’ => ONETHINK_ADDON_PATH, ‘OSS’ => VENDOR_PATH.’OSS’), //扩展模块列表

Register the new namespace OSS
Register the new namespace OSS

4. When instantiating the libraries under OSS, such as

new OSS\OssClient();

the corresponding library file OssClient.class.php is automatically loaded. Therefore, you need to rename the files in the OSS directory, changing OssClient.php to OssClient.class.php. All files in the OSS directory should be processed in this way, as shown in Figure 4

Rename the files in the OSS directory, changing OssClient.php to OssClient.class.php. All files in the OSS directory should be processed in this way
Rename the files in the OSS directory, changing OssClient.php to OssClient.class.php. All files in the OSS directory should be processed in this way

5. Edit passport.hmwis.com\Application\Common\Conf\config.php to set the OSS data center access domain name for the production environment to the internal network, as shown in Figure 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
),

Set the OSS data center access domain name for the production environment to the internal network
Set the OSS data center access domain name for the production environment to the internal network

6. Edit passport.hmwis.com\Application\Common\Conf\debug.php to set the OSS data center access domain name for the development environment to the external network, as shown in Figure 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
),

Set the OSS data center access domain name for the development environment to the external network
Set the OSS data center access domain name for the development environment to the external network

7. Implement the function of uploading local files to OSS, such as changing an avatar, as shown in Figure 7:

Implement the function of uploading local files to OSS, such as changing an avatar
Implement the function of uploading local files to OSS, such as changing an avatar

8. Edit passport.hmwis.com\Application\Home\Controller\ProfileController.class.php to import the OSS client class and exception class, as shown in Figure 8
use OSS\OssClient;
use OSS\Core\OssException;

Import the OSS client class and exception class
Import the OSS client class and exception class

9. Instantiate the OssClient class and call the local file upload method, as shown in Figure 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);
}

Instantiate the OssClient class and call the local file upload method
Instantiate the OssClient class and call the local file upload method

10. The ThinkPHP file upload operation is customized based on the Think\Upload class to implement uploading files to OSS via a form. Copy passport.hmwis.com\ThinkPHP\Library\Think\Upload.class.php to passport.hmwis.com\ThinkPHP\Library\Vendor\OSS\OssUpload.class.php, and edit passport.hmwis.com\ThinkPHP\Library\Vendor\OSS\OssUpload.class.php, as shown in Figure 10

namespace OSS;
use OSS\OssClient;
use OSS\Core\OssException;

Customize the ThinkPHP file upload operation using the Think\Upload class to implement uploading files to OSS via a form
Customize the ThinkPHP file upload operation using the Think\Upload class to implement uploading files to OSS via a form

11. Edit the default upload configuration, as shown in Figure 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, //检测文件是否存在回调,如果存在返回文件信息数组
);

Note: The main change is removing the implementation of the file upload driver and file directory-related operations. Due to the extensive modifications, it is recommended to check the SVN log or directly copy OssUpload.class.php;

The main change is removing the implementation of the file upload driver and file directory-related operations. Due to the extensive modifications, it is recommended to check the SVN log or directly copy OssUpload.class.php
The main change is removing the implementation of the file upload driver and file directory-related operations. Due to the extensive modifications, it is recommended to check the SVN log or directly copy OssUpload.class.php

12. Edit the upload file method `upload`, as shown in Figure 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();
}

Edit the upload file method upload
Edit the upload file method upload

13. To implement uploading files to OSS via a form, edit passport.hmwis.com\Application\Home\Controller\ProfileController.class.php and import the OSS file upload class
use OSS\OssUpload;
For all upload implementations, it is recommended to refer to the documentation for the Think\Upload class, as they are essentially identical

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系