您的位置:首页 > 编程语言 > PHP开发

php 关于使用七牛云存储

2015-07-27 12:11 549 查看
1.首先注册七牛云存储账号 http://www.qiniu.com/
2.获得密钥3.仔细查看文档
http://developer.qiniu.com/docs/v6/sdk/php-sdk.html#io-put-flow
4.下载sdk 可以使用composer,也可以直接使用压缩包进行下载5.在项目中引入七牛的类包6.获得token代码:<?php
require_once 'autoload.php';use Qiniu\Auth;$accessKey = 'ntL5AciwhaAa35APXKCSlC4KoUKyN77KNPmbHW0K';
$secretKey = 'x5W3KQikAzHTBYRdezWSMY9XGn0MLR0GQLXRd6X1';
$auth = new Auth($accessKey, $secretKey);$bucket = 'bucket';
$token = $auth->uploadToken($bucket);
?>结果:string
'ntL5AciwhaAa35APXKCSlC4KoUKyN77KNPmbHW0K:H_W87vY-abWaHvOKpzVNGdwNUbc=:eyJzY29wZSI6ImJ1Y2tldCIsImRlYWRsaW5lIjoxNDM3OTc3ODAyfQ==' (length=126)

7.上传字符串代码:<?php
require_once 'autoload.php';use Qiniu\Auth;
use Qiniu\Storage\UploadManager;$accessKey = '你的accessKey';
$secretKey = '你的secretKey';
$auth = new Auth($accessKey, $secretKey);$bucket = 'bucket';// 设置put policy的其他参数, 上传回调
//$opts = array(
// 'callbackUrl' => 'http://www.callback.com/',
// 'callbackBody' => 'name=$(fname)&hash=$(etag)'
// );
//$token = $auth->uploadToken($bucket, null, 3600, $opts);$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();list($ret, $err) = $uploadMgr->put($token, null, 'content string');
echo "\n====> put result: \n";
if ($err !== null) {
var_dump($err);
} else {
var_dump($ret);
}
?>结果:====> put result:
array (size=2)
'hash' =>

string
'FkRvouCaQN6HmCyPmMuBd0OnhiOi' (length=28)
'key' =>

string
'FkRvouCaQN6HmCyPmMuBd0OnhiOi' (length=28)

8.上传文件
代码:

<?php
require_once 'autoload.php';

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

$accessKey = '你的accessKey';
$secretKey = '你的secretKey';
$auth = new Auth($accessKey, $secretKey);

$bucket = 'bucket';

// 设置put policy的其他参数, 上传回调
//$opts = array(
// 'callbackUrl' => 'http://www.callback.com/',
// 'callbackBody' => 'name=$(fname)&hash=$(etag)'
// );
//$token = $auth->uploadToken($bucket, null, 3600, $opts);

$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();

list($ret, $err) = $uploadMgr->putFile($token, null, "desert.jpg");
echo "\n====> putFile result: \n";
if ($err !== null) {
var_dump($err);
} else {
var_dump($ret);
}
?>
结果:
====> putFile result:

array (size=2)
'hash' =>

string

'FjBCDRqa-yvLYDNYElaa9ENaWc4X' (length=28)
'key' =>

string

'FjBCDRqa-yvLYDNYElaa9ENaWc4X' (length=28)

9.下载图片(私有,如果公有的话,不用key值)

<?php
require_once 'autoload.php';use Qiniu\Auth;$accessKey = '你的accessKey';
$secretKey = '你的secretKey';
$auth = new Auth($accessKey, $secretKey);$baseUrl = 'http://7xkofd.com1.z0.glb.clouddn.com/FtmX4cN-3AWth9A2A-Mq1JXuLPzh';
$authUrl = $auth->privateDownloadUrl($baseUrl);
function download_remote_file_with_curl($file_url, $save_to)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_URL,$file_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file_content = curl_exec($ch);
curl_close($ch);$downloaded_file = fopen($save_to, 'w');
fwrite($downloaded_file, $file_content);
fclose($downloaded_file);}
download_remote_file_with_curl($authUrl, time().'file.jpg')
?>

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: