123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- namespace app\common\components;
- define('__BOS_CLIENT_ROOT', dirname(__DIR__));
- include BASE_PATH.'components'.DIRECTORY_SEPARATOR.'bce'.DIRECTORY_SEPARATOR.'BaiduBce.phar';
- use OSS\OssClient;
- use OSS\Core\OssException;
- use Aws\S3\S3Client;
- use Aws\S3\MultipartUploader;
- use Aws\S3\Exception\S3Exception;
- use Aws\Exception\AwsException;
- use Aws\Exception\MultipartUploadException;
- use Aws\S3\ObjectUploader;
- use \BaiduBce\BceClientConfigOptions;
- use \BaiduBce\Util\MimeTypes;
- use \BaiduBce\Http\HttpHeaders;
- use \BaiduBce\Services\Bos\BosClient;
- use Yii;
- class Oss
- {
- public $ossClient;
- public $ossUrl;
- public $ossInerUrl;
- public $accessKeyId;
- public $accessKeySecret;
- public $endpoint;
- public $bucket;
- public $region;
- public $type;
- public $cname;
- public function init($internal=0)
- {
- $this->ossUrl = getOssUrl();
- $this->ossInerUrl = getOssInterUrl();
- $this->accessKeyId = Yii::$app->params['oss']['OSS_ACCESS_KEY'];
- $this->accessKeySecret = Yii::$app->params['oss']['OSS_ACCESS_SECRET'];
- $this->endpoint = $internal?Yii::$app->params['oss']['OSS_INTERNAL_ENDPOINT']:Yii::$app->params['oss']['OSS_ENDPOINT'];
- $this->bucket = Yii::$app->params['oss']['OSS_BUCKET'];
- $this->region = Yii::$app->params['oss']['OSS_REGION'];
- $this->type = Yii::$app->params['oss']['OSS_TYPE'];
- $this->cname = FALSE;
- $result = [];
- if($this->type=='ali')
- {
- try {
- $this->ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint,$this->cname);
- $result = array('error'=>0);
- } catch (OssException $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- else if($this->type=='tencent')
- {
- $protocol = str_replace("://","",SITE_PROTOCOL);
- $this->ossClient = new \Qcloud\Cos\Client(
- array(
- 'region' => $this->region,
- 'schema' => $protocol, //协议头部,默认为http
- 'credentials'=> array(
- 'secretId' => $this->accessKeyId ,
- 'secretKey' => $this->accessKeySecret)));
- $result = array('error'=>0);
- }
- else if($this->type=='baidu')
- {
- $this->ossClient = new BosClient(array(
- 'credentials' => array(
- 'accessKeyId' => $this->accessKeyId,
- 'secretAccessKey' => $this->accessKeySecret,
- ),
- 'endpoint' => $this->endpoint
- ));
- $result = array('error'=>0);
- }
- else if($this->type=='s3')
- {
- $this->ossClient = new S3Client([
- 'version' => 'latest',
- 'region' => $this->region,
- 'endpoint' => 'https://' . str_replace(array('https://','http://'),'',$this->endpoint),
- 'credentials' => [
- 'key' => $this->accessKeyId,
- 'secret' => $this->accessKeySecret,
- ]
- ]);
- }
- return $result;
- }
- /*
- * $file:完整文件路径
- * $object:文件存放到OSS的相对路径
- */
- public function Upload($file,$object)
- {
- $result = [];
- $data = [];
- if($this->type=='ali')
- {
- try{
- $ossresult = $this->ossClient->uploadFile($this->bucket, $object, $file);
- $ossresult['info']['url'] = str_replace(Yii::$app->params['oss']['OSS_INTERNAL_ENDPOINT'],Yii::$app->params['oss']['OSS_ENDPOINT'],$ossresult['info']['url']);
- $data['url'] = str_replace(array('https://','http://'),SITE_PROTOCOL,$ossresult['info']['url']);
- $data['path'] = $object;
- $data['size'] = $ossresult['info']['upload_content_length'];
- $result = array('error'=>0,'data'=>$data);
- } catch(OssException $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- else if($this->type=='tencent')
- {
- try {
- $fileContent = fopen($file, "rb");
- if ($fileContent) {
- $ossresult = $this->ossClient->putObject(array(
- 'Bucket' => $this->bucket,
- 'Key' => $object,
- 'Body' => $fileContent));
- $data['url'] = SITE_PROTOCOL.$ossresult['Location'];
- $data['path'] = $object;
- $data['size'] = filesize($file);
- $result = array('error'=>0,'data'=>$data);
- }
- } catch (\Exception $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- else if($this->type=='baidu')
- {
- try {
- $this->ossClient->putObjectFromFile($this->bucket,$object,$file);
- $data['url'] = SITE_PROTOCOL.$this->bucket.'.'.$this->endpoint.'/'.$object;
- $data['path'] = $object;
- $data['size'] = filesize($file);
- $result = array('error'=>0,'data'=>$data);
- } catch (\Exception $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- else if($this->type=='s3')
- {
- try {
- $key = $object;
- $source = fopen($file, 'rb');
- $acl = 'public-read';
- $uploader = new ObjectUploader(
- $this->ossClient,
- $this->bucket,
- $key,
- $source,
- $acl
- );
- $ossresult = $uploader->upload();
- if ($ossresult["@metadata"]["statusCode"] == '200') {
- $data['url'] = $ossresult["ObjectURL"];
- $data['path'] = $object;
- $data['size'] = filesize($file);
- $result = array('error'=>0,'data'=>$data);
- }
- } catch (\Exception $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- return $result;
- }
- /**
- * @param $localfile
- * @param $object
- */
- public function downLoad($localfile,$object)
- {
- $object = str_replace($this->ossUrl,'',$object);
- $object= str_replace($this->ossInerUrl,'',$object);
- $result = [];
- if($this->type=='ali')
- {
- try{
- $options = array(
- OssClient::OSS_FILE_DOWNLOAD => $localfile
- );
- $this->ossClient->getObject($this->bucket, $object, $options);
- $result = array('error'=>0);
- } catch(OssException $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- else if($this->type=='tencent')
- {
- try {
- $key = $object; //此处的 key 为对象键,对象键是对象在存储桶中的唯一标识
- $this->ossClient->getObject(array('Bucket' => $this->bucket,'Key' => $key,'SaveAs' => $localfile));
- $result = array('error'=>0);
- } catch (\Exception $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- else if($this->type=='baidu')
- {
- try {
- $this->ossClient->getObjectToFile($this->bucket, $object, $localfile);
- $result = array('error'=>0);
- } catch (\Exception $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- else if($this->type=='s3')
- {
- try {
- $result = $this->ossClient->getObject([
- 'Bucket' => $this->bucket,
- 'Key' => $object
- ]);
- //header("Content-Type: {$result['ContentType']}");
- file_put_contents($localfile,$result['Body']);
- $result = array('error'=>0);
- } catch (S3Exception $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- return $result;
- }
- //删除单个文件
- public function delete($object){
- $object = str_replace($this->ossUrl,'',$object);
- $object= str_replace($this->ossInerUrl,'',$object);
- if($this->type=='ali')
- {
- try{
- $ossresult = $this->ossClient->deleteObject($this->bucket, $object);
- $result = array('error'=>0,'data'=>'删除成功');
- } catch(OssException $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- else if($this->type=='tencent')
- {
- try {
- $ossresult = $this->ossClient->deleteObject(array(
- 'Bucket' => $this->bucket, //存储桶名称
- 'Key' => $object
- ));
- $result = array('error'=>0,'data'=>'删除成功');
- } catch (\Exception $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- else if($this->type=='baidu')
- {
- try {
- $ossresult = $this->ossClient->deleteObject($this->bucket, $object);
- $result = array('error'=>0,'data'=>'删除成功');
- } catch (\Exception $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- else if($this->type=='s3')
- {
- try {
- $ossresult = $this->ossClient->deleteObject([
- 'Bucket' => $this->bucket,
- 'Key' => $object
- ]);
- $result = array('error'=>0,'data'=>'删除成功');
- } catch (S3Exception $e) {
- $result = array('error'=>1,'msg'=>$e->getMessage());
- }
- }
- return $result;
- }
-
- }
- ?>
|