CosService.php 3.2 KB
<?php

namespace App\Services;

use App\Utils\LogUtils;
use Qcloud\Cos\Client;
/**
 * @remark :对象存储cos
 * @class  :CosService.php
 * @author :lyh
 * @time   :2023/7/19 15:09
 */
class CosService
{

    /**
     * @param $file
     * @remark :上传图片
     * @name   :uploadFile
     * @author :lyh
     * @method :post
     * @time   :2023/7/19 15:28
     */
    public function uploadFile(&$files,$path,$filename)
    {
        $cos = config('filesystems.disks.cos');
        $cosClient = new Client([
            'region' => $cos['region'],
            'credentials' => [
                'secretId' => $cos['credentials']['secretId'],
                'secretKey' => $cos['credentials']['secretKey'],
            ],
        ]);
        $key = $path.'/'.$filename;
        $cosClient->putObject([
            'Bucket' => $cos['bucket'],
            'Key' => $key,
            'Body' => fopen($files->getRealPath(), 'r'),
        ]);
        return $key;
    }

    /**
     * @param $image_name
     * @remark :获取图片访问链接
     * @name   :getImageUrl
     * @author :lyh
     * @method :post
     * @time   :2023/7/19 16:08
     */
    public function getImageUrl($image_name)
    {
        $cos = config('filesystems.disks.cos');
        $cosClient = new Client([
            'region' => $cos['region'],
            'credentials' => [
                'secretId' => $cos['credentials']['secretId'],
                'secretKey' => $cos['credentials']['secretKey'],
            ],
        ]);
        $imageUrl = $cosClient->getObjectUrl($cos['bucket'], trim($image_name,'/'), '+10 years');
        return $imageUrl;
    }


    /**
     * 根据远程图片地址上传
     * @param $project_id
     * @param $image_type
     * @param $file_url
     * @param $key
     * @return string
     * @author Akun
     * @date 2023/09/21 9:39
     */
    public static function uploadRemote($project_id,$image_type,$file_url,$key='')
    {
        if(!$key){
            $ext = explode('.',$file_url);

            $filename = uniqid().rand(10000,99999).'.'.end($ext);

            $uploads = config('upload.default_file');
            $path = $uploads['path_b'].'/'.$project_id.'/'.$image_type.'/'.date('Y-m');
            $key = $path.'/'.$filename;
        }

        $cos = config('filesystems.disks.cos');
        $cosClient = new Client([
            'region' => $cos['region'],
            'credentials' => [
                'secretId' => $cos['credentials']['secretId'],
                'secretKey' => $cos['credentials']['secretKey'],
            ],
        ]);

        $opts = [
            'http' => [
                'header' => 'User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0'
            ],
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ]
        ];
        $body = file_get_contents($file_url,false,stream_context_create($opts));

        try {
            $cosClient->putObject([
                'Bucket' => $cos['bucket'],
                'Key' => $key,
                'Body' => $body,
            ]);
            return $key;
        }catch (\Exception $e){
            LogUtils::error('uploadRemote error', $e->getMessage());
            return '';
        }
    }

}