xingkoo / yii2-qiniu-sdk
为Yii 2.0提供的七牛云存储API SDK
dev-master
2019-04-17 12:34 UTC
Requires
- php: >=5.4.0
- qiniu/php-sdk: v7.0.5
- yiisoft/yii2: *
Requires (Dev)
- phpunit/phpunit: >=4.0
This package is auto-updated.
Last update: 2024-09-18 00:27:01 UTC
README
基于Yii2实现的七牛云存储API SDK(使用官方SDK)(目前处于开发中)
环境要求
-
>= PHP 5.4
-
>= Yii 2.0
- cURL扩展
安装
在composer.json文件中添加以下代码,并执行composer update --no-dev命令
{
"require": {
"xingkoo/yii2-qiniu-sdk": "dev-master"
}
}
设置方法
// 全局使用 // 在config/main.php配置文件中定义component配置信息 'components' => [ ..... 'qiniu' => [ 'class' => 'xingkoo\Qiniu\Qiniu', 'accessKey' => 'Access Key', 'secretKey' => 'Secret Key', 'domain' => '七牛域名', 'bucket' => '空间名', 'secure' => false, // 是否使用HTTPS,默认为false ] .... ] // 代码中调用 $result = Yii::$app->qiniu->putFile('img/test.jpg', __DIR__.'/test.jpg'); ....
// 局部调用 $qiniu = Yii::createObject([ 'class' => 'xingkoo\Qiniu\Qiniu', 'accessKey' => 'Access Key', 'secretKey' => 'Secret Key', 'domain' => '七牛域名', 'bucket' => '空间名', 'secure' => false, // 是否使用HTTPS,默认为false ]); $result = $qiniu->putFile('img/test.jpg', __DIR__.'/test.jpg'); ....
使用示例
通过路径上传文件
$ret = Yii::$app->qiniu->putFile('img/test.jpg', __DIR__.'/test.jpg'); if ($ret['code'] === 0) { // 上传成功 $url = $ret['result']['url']; // 目标文件的URL地址,如:http://[七牛域名]/img/test.jpg } else { // 上传失败 $code = $ret['code']; // 错误码 $message = $ret['message']; // 错误信息 }
通过内容上传文件
$fileData = file_get_contents(__DIR__.'/test.jpg'); $ret = Yii::$app->qiniu->put('img/test.jpg', $fileData); if ($ret['code'] === 0) { // 上传成功 $url = $ret['result']['url']; // 目标文件的URL地址,如:http://[七牛域名]/img/test.jpg } else { // 上传失败 $code = $ret['code']; // 错误码 $message = $ret['message']; // 错误信息 }
获取私有文件下载链接
$fileList = [ 'http://domain/private-file1.jpg', 'http://domain/private-file2.jpg', 'http://domain/private-file3.jpg', ]; $urlMaps = Yii::$app->qiniu->batchDownload($fileList); foreach ($urlMaps as $fileUrl => $downloadUrl) { // TODO }
$download = Yii::$app->qiniu->download($fileName);
获取上传凭证
$bucket = 'test_bucket'; $key = null; $expires = 7200; $policy = null; $token = Yii::$app->qiniu->uploadToken($bucket, $key, $expires, $policy); // TODO