阿里云、七牛云、腾讯云远程附件php简单示例

一、阿里云

/**
 * Created by PhpStorm.
 * User: Mr.Yang
 * Date: 2022/4/13
 * Time: 10:14
 * QQ: 2575404985
 */

namespace app\api\controller;

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

require_once __DIR__ . '/../../../vendor/alioss/autoload.php';

class Oss {
    private $ossClient = null;
    private $accessKeyId = null;
    private $accessKeySecret = null;
    private $endpoint = null;
    private $bucket = null;
    public function __construct($accessKeyId, $accessKeySecret, $endpoint, $bucket)
    {
        $this->accessKeyId = $accessKeyId;
        $this->accessKeySecret = $accessKeySecret;
        $this->endpoint = $endpoint;
        $this->bucket = $bucket;
        if (!$this->ossClient){
            try{
                $this->ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            } catch(OssException $e) {
                Send::returnMsg(-2, $e->getMessage());
            }
        }
    }

    /**
     * 上传文件
     * @param $path
     * @return null
     * @throws OssException
     */
    public function uploadFile($path)
    {
        $this->ossClient->uploadFile($this->bucket, $path, __ROOT__.'/'.$path);
        @unlink(__ROOT__.'/'.$path);
        return true;
    }

    /**
     * 获取bucket列表
     * @return array
     * @throws OssException
     */
    public function getBucketList()
    {
        $bucketListInfo  = $this->ossClient->listBuckets();
        $bucketList = $bucketListInfo->getBucketList();
        $bucket_array = array();
        foreach($bucketList as $bucket) {
            $name = $bucket->getName();
            $location = $bucket->getLocation();
            $bucket_array[] = array(
                'name' => $bucket->getName(),
                'bucket' => $name.'@'.$location
            );
        }
        return $bucket_array;
    }

    /**
     * 删除文件
     * @param $path
     * @return null
     */
    public function delFile($path)
    {
        return $this->ossClient->deleteObject($this->bucket, $path);
    }
}

二、七牛云

/**
 * Created by PhpStorm.
 * User: Mr.Yang
 * Date: 2022/4/13
 * Time: 14:33
 * QQ: 2575404985
 */

namespace app\api\controller;

use Qiniu\Auth;
use Qiniu\Config;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

require_once __DIR__ . '/../../../vendor/qiniu/autoload.php';
class Qiniu
{
    private $accesskey = null;
    private $secretkey = null;
    private $bucket = null;
    private $auth = null;

    public function __construct($accesskey, $secretkey, $bucket)
    {
        $this->accesskey = $accesskey;
        $this->secretkey = $secretkey;
        $this->bucket = $bucket;
        if (!$this->auth){
            $this->auth = new Auth($accesskey, $secretkey);
        }
    }

    /**
     * 删除远程文件
     * @param $path
     * @return bool
     * @throws \Exception
     */
    public function uploadFile($path)
    {
        // 生成上传 Token
        $token = $this->auth->uploadToken($this->bucket);
        $uploadMgr = new UploadManager();
        $filePath = __ROOT__.'/'.$path;
        list($ret, $err) = $uploadMgr->putFile($token, $path, $filePath);
        if ($err !== null) {
            return false;
        } else {
            @unlink(__ROOT__.'/'.$path);
            return true;
        }
    }

    /**
     * 删除远程文件
     * @param $path
     * @return array
     */
    public function delFile($path)
    {
        $config = new Config();
        $bucketManager = new BucketManager($this->auth, $config);
        return $bucketManager->delete($this->bucket, $path);
    }
}

三、腾讯云

/**
 * Created by PhpStorm.
 * User: Mr.Yang
 * Date: 2022/4/13
 * Time: 11:11
 * QQ: 2575404985
 */

namespace app\api\controller;

require_once __DIR__ . '/../../../vendor/tencent-cos/index.php';

class Cos
{
    private $cosClient = null;
    private $secretId = null;
    private $secretKey = null;
    private $region = null;
    private $bucket = null;
    private $host = null;

    public function __construct($secretId, $secretKey, $region, $bucket, $host = '')
    {

        $this->secretId = $secretId; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
        $this->secretKey = $secretKey; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi
        $this->region = $region; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
        $this->bucket = $bucket; //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
        $this->host = $host;

        if (!$this->cosClient) {
            $config = array(
                //'domain' => $host,
                'region' => $this->region,
                'schema' => 'http', //协议头部,默认为http
                'credentials' => array(
                    'secretId' => $this->secretId,
                    'secretKey' => $this->secretKey
                )
            );
            $this->cosClient = new \Qcloud\Cos\Client($config);
        }
    }

    /**
     * 上传文件
     * @param $path
     * @return object
     */
    public function uploadFile($path)
    {
        $this->cosClient->putObject(array(
            'Bucket' => $this->bucket, //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
            'Key' => $path,
            'Body' => fopen(__ROOT__ . '/' . $path, 'rb')
        ));
        //删除本地文件
        @unlink(__ROOT__.'/'.$path);
    }

    /**
     * 删除文件
     * @param $path
     * @return object
     */
    public function delFile($path)
    {
        return $this->cosClient->deleteObject(array(
            'Bucket' => $this->bucket, //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
            'Key' => $path
        ));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

样子2018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值