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
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:
string
string
<?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);
}
?>
<?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')
?>
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')
?>
相关文章推荐
- Laravel
- laravel陌生知识点快速学习(一)
- Ubuntu14.04 搭建FTP
- php实现输出数组全排列
- ThinkPHP控制器详解
- php数组
- php实现通过图的深度优先遍历输出1,2,3...n的全排列
- ntpdate[1788]: the NTP socket is in use, exiting
- JRTPLib的编译步骤
- ThinkPHP路由详解
- ThinkPHP安装和设置
- PHP根据经纬度计算两点间实际距离
- 纯 PHP 代码最好在文件末尾删除 PHP 结束标记
- zendstudio 声明变量类型,让变量自动方法提示
- 教你在PHPStorm中配置Xdebug
- php+正则将字符串中的字母数字和中文分割
- Windows 与开发板之间使用tftp服务不通的问题
- php操作sql server 驱动
- 关于PHP开发的9条建议
- 在 Windows 上快速安装并运行 Laravel 5.x