123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace addon\alioss\model;
- use app\model\BaseModel;
- use \OSS\OssClient;
- use \OSS\Core\OssException;
- class Alioss extends BaseModel
- {
-
- public function put($param){
- $data = $param["data"];
- $key = $param["key"];
- $config_model = new Config();
- $config_result = $config_model->getAliossConfig();
- $config = $config_result["data"];
- if($config["is_use"] == 1){
- $config = $config["value"];
- $access_key_id = $config["access_key_id"];
- $access_key_secret = $config["access_key_secret"];
- $bucket = $config["bucket"];
- $endpoint = $config["endpoint"];
- try{
- $ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
- $result = $ossClient->putObject($bucket, $key, $data);
- $data = array(
-
- "path" => $result["info"]["url"],
- "domain" => $endpoint,
- "bucket" => $bucket
- );
- return $this->success($data);
- } catch(OssException $e) {
- return $this->error($e->getMessage());
- }
- }
- }
-
- public function putFile($param){
- $file_path = $param["file_path"];
- $key = $param["key"];
- $config_model = new Config();
- $config_result = $config_model->getAliossConfig();
- $config = $config_result["data"];
- if($config["is_use"] == 1) {
- $config = $config["value"];
- $access_key_id = $config["access_key_id"];
- $access_key_secret = $config["access_key_secret"];
- $bucket = $config["bucket"];
-
- $endpoint = $config["endpoint"];
- try{
- $ossClient = new OssClient($access_key_id, $access_key_secret, $endpoint);
- $result = $ossClient->uploadFile($bucket, $key, $file_path);
-
- $data = array(
- "path" => $result["info"]["url"],
- "domain" => $endpoint,
- "bucket" => $bucket
- );
- return $this->success($data);
- } catch(OssException $e) {
- return $this->error($e->getMessage());
- }
- }
- }
- public function subEndpoint($endpoint, $bucket){
- if (strpos($endpoint, 'http://') === 0 ) {
- $temp = "http://";
- }else{
- $temp = "https://";
- }
- $temp_array = explode($temp, $endpoint);
- return $temp.$bucket.".".$temp_array[1];
- }
- }
|