上传文件到空间
表单上传
<?php
require_once __DIR__ . '/autoload.php';
use Qiniu\Auth;
$accessKey = ('ak');
$secretKey = ('sk');
$bucket = ('bk');
$auth = new Auth($accessKey, $secretKey);
$upToken = $auth->uploadToken($bucket);
?>
<form method="post" action="http://up-z1.qiniu.com" enctype="multipart/form-data">
<input name="token" type="hidden" value="<?php echo $upToken; ?>">
<input name="file" type="file" />
<input type="submit" value="上传"/>
</form>
指定上传
<?php
require_once __DIR__ . '/autoload.php';
// 引入鉴权类
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;
// 需要填写你的 Access Key 和 Secret Key
$accessKey = ('ak');
$secretKey = ('sk');
$bucket = ('bk');
// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
// 生成上传 Token
$token = $auth->uploadToken($bucket);
// 要上传文件的本地路径
$filePath = './php-logo.png';
// 上传到七牛后保存的文件名
$key = 'my-php-logo.png';
// 初始化 UploadManager 对象并进行文件的上传。
$uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传。
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
echo "\n====> putFile result: \n";
if ($err !== null) {
var_dump($err);
} else {
var_dump($ret);
}
?>
获取空间文件
<?php
require_once __DIR__ . '/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
$accessKey = ('ak');
$secretKey = ('sk');
$bucket = ('bk');
$auth = new Auth($accessKey, $secretKey);
$bucketManager = new BucketManager($auth);
// 要列取文件的公共前缀
$prefix = '';
// 上次列举返回的位置标记,作为本次列举的起点信息。
$marker = '';
// 本次列举的条目数
$limit = 100;
$delimiter = '/';
// 列举文件
list($ret, $err) = $bucketManager->listFiles($bucket, $prefix, $marker, $limit, $delimiter);
if ($err !== null) {
echo "\n====> list file err: \n";
var_dump($err);
} else {
if (array_key_exists('marker', $ret)) {
echo "Marker:" . $ret["marker"] . "\n";
}
#print_r($ret['items']);
$attr=$ret['items'];
echo '--------files----------<br>';
foreach($attr as $v)
{
echo ("file:{$v['key']}    size:{$v['fsize']}<br>");
}
}
?>
删除文件
<?php
require_once __DIR__ . '/autoload.php';
use \Qiniu\Auth;
// 需要填写你的 Access Key 和 Secret Key
$accessKey = ('ak');
$secretKey = ('sk');
$bucket = ('bk');
$key = "my-php-logo.png";
$auth = new Auth($accessKey, $secretKey);
$config = new \Qiniu\Config();
$bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
$err = $bucketManager->delete($bucket, $key);
if ($err) {
print_r($err);
}
else
echo 'del complete'
下载私有文件
<?php
require_once __DIR__ . '//autoload.php';
use Qiniu\Auth;
$accessKey = ('ak');
$secretKey = ('sk');
// 构建Auth对象
$auth = new Auth($accessKey, $secretKey);
// 私有空间中的外链 http://<domain>/<file_key>
$baseUrl = 'http://qny.ycwjwl.com/my-php-logo.png';
// 对链接进行签名
$signedUrl = $auth->privateDownloadUrl($baseUrl);
echo "<a href='{$signedUrl}'>download</a>";
整合
<?php
require_once __DIR__ . '/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
$accessKey = ('ak');
$secretKey = ('sk');
$bucket = ('bk');
$url='http://qny.ycwjwl.com/';
$auth = new Auth($accessKey, $secretKey);
$bucketManager = new BucketManager($auth);
// 要列取文件的公共前缀
$prefix = '';
// 上次列举返回的位置标记,作为本次列举的起点信息。
$marker = '';
// 本次列举的条目数
$limit = 100;
$delimiter = '/';
// 列举文件
list($ret, $err) = $bucketManager->listFiles($bucket, $prefix, $marker, $limit, $delimiter);
if ($err !== null) {
echo "\n====> list file err: \n";
var_dump($err);
} else {
if (array_key_exists('marker', $ret)) {
echo "Marker:" . $ret["marker"] . "\n";
}
// print_r($ret['items']);
$attr=$ret['items'];
echo '--------files----------<br>';
foreach($attr as $v)
{
echo ("<a href='{$url}{$v['key']}'>file:{$v['key']}</a>    type:{$v['mimeType']}   size:{$v['fsize']}   time:{$v['putTime']}  <a href='?key={$v['key']}'>del</a><br>");
}
}
#del
if(isset($_GET['key'])){
$key = $_GET['key'];
$auth = new Auth($accessKey, $secretKey);
$config = new \Qiniu\Config();
$bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
$err = $bucketManager->delete($bucket, $key);
if ($err) {
print_r($err);
}
else{
echo '<script>aleart("del complete")</script>';
header("location:/");
}
}
#send
$upToken = $auth->uploadToken($bucket);
?>
<form method="post" action="http://up-z1.qiniu.com" enctype="multipart/form-data">
<input name="token" type="hidden" value="<?php echo $upToken; ?>">
<input name="file" type="file" />
<input type="submit" value="上传"/>
</form>
抓取网络文件到空间
<?php
require_once __DIR__ . '/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
$accessKey = ('ak');
$secretKey = ('sk');
$bucket = ('bk');
$auth = new Auth($accessKey, $secretKey);
$bucketManager = new BucketManager($auth);
$url = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token=6KU46ONsZfidOn3JoK3ARDTu9WbXTxh7OMfMlvmshumvUqPl9pESGVg_suo1dxvhII0Npqvo8zbqQD5dZbYCwXruky2S-EBE3bavqTEEDzxciDEXSD6JJpHhU_15xSmlRYScAJAFMG&media_id=ip2w33lqavOZ-Tiql8NzGV_hkEke_FSelY0fI1sUD_d3CAo0AO21lqjatiaFGvtG'; //被抓取的文件
$key = time() . '.amr'; //保存在空间的名字
// 指定抓取的文件保存名称
list($ret, $err) = $bucketManager->fetch($url, $bucket, $key);
echo "=====> fetch $url to bucket: $bucket key: $key\n";
if ($err !== null) {
var_dump($err);
} else {
print_r($ret);
}
// 不指定key时,以文件内容的hash作为文件名
$key = null;
list($ret, $err) = $bucketManager->fetch($url, $bucket, $key);
echo "=====> fetch $url to bucket: $bucket key: $(etag)\n";
if ($err !== null) {
var_dump($err);
} else {
print_r($ret);
}
文件转码
<?php
require_once __DIR__ . '//autoload.php';
use Qiniu\Auth;
use Qiniu\Processing\PersistentFop;
//对已经上传到七牛的视频发起异步转码操作
$accessKey = ('ak');
$secretKey = ('sk');
$bucket = ('bk');
$auth = new Auth($accessKey, $secretKey);
//要转码的文件所在的空间和文件名。
$key = '1508920540.amr';
//转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
$pipeline = 'fenda';
$force = false;
//转码完成后通知到你的业务服务器。
$notifyUrl = '';
$config = new \Qiniu\Config();
//$config->useHTTPS=true;
$pfop = new PersistentFop($auth, $config);
//要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
$fops = "avthumb/mp3/ab/320k/ar/44100/acodec/libmp3lame|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":1.mp3");//将amr文件转为MP3文件
list($id, $err) = $pfop->execute($bucket, $key, $fops, $pipeline, $notifyUrl, $force);
echo "\n====> pfop avthumb result: \n";
if ($err != null) {
var_dump($err);
} else {
echo "PersistentFop Id: $id\n";
}
//查询转码的进度和状态
list($ret, $err) = $pfop->status($id);
echo "\n====> pfop avthumb status: \n";
if ($err != null) {
var_dump($err);
} else {
var_dump($ret);
}