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


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’), //扩展模块列表

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

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
),

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
),

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

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;

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);
}

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;

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;

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();
}

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
