yuyangame/yii2-qiniu-sdk

Qiniu API for Yii 2.0

dev-yuyangame 2017-07-06 07:05 UTC

This package is not auto-updated.

Last update: 2024-05-21 15:54:54 UTC


README

基于Yii2实现的七牛云存储API SDK(使用官方SDK)(目前开发中) 原作者 @chocoboxxf (我只是添加了自己要用的API,方便自己使用而已)

添加

  • download 单文件下载
  • more features coming soon

环境条件

  • = PHP 5.4

  • = Yii 2.0

  • cURL extension

安装

添加下列代码在composer.json文件中并执行composer update --no-dev操作

{
    "require": {
       "yuyangame/yii2-qiniu-sdk": "dev-master"
    }
}

设置方法

// 全局使用
// 在config/main.php配置文件中定义component配置信息
'components' => [
  .....
  'qiniu' => [ 
      'class' => 'yuyangame\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' => 'yuyangame\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
}

// or
$fileName = 'http://domain/private-file1.jpg';
$url = Yii::$app->qiniu->download($fileName);

获取上传凭证

$bucket = 'test_bucket';
$key = null;
$expires = 7200;
$policy = null;
$token = Yii::$app->qiniu->uploadToken($bucket, $key, $expires, $policy);
// TODO