1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324 |
- <?php
- namespace Qiniu\Storage;
- use Qiniu\Auth;
- use Qiniu\Config;
- use Qiniu\Http\Error;
- use Qiniu\Http\Client;
- use Qiniu\Http\Proxy;
- use Qiniu\Http\Response;
- /**
- * 主要涉及了空间资源管理及批量操作接口的实现,具体的接口规格可以参考
- *
- * @link https://developer.qiniu.com/kodo/api/1274/rs
- */
- final class BucketManager
- {
- private $auth;
- private $config;
- private $proxy;
- public function __construct(
- Auth $auth,
- Config $config = null,
- $proxy = null,
- $proxy_auth = null,
- $proxy_user_password = null
- ) {
- $this->auth = $auth;
- if ($config == null) {
- $this->config = new Config();
- } else {
- $this->config = $config;
- }
- $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
- }
- /**
- * 获取指定账号下所有的空间名
- *
- * @param bool $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
- * @return array 包含所有空间名
- */
- public function buckets($shared = true)
- {
- $includeShared = "false";
- if ($shared === true) {
- $includeShared = "true";
- }
- return $this->getV2($this->config->getUcHost() . '/buckets?shared=' . $includeShared);
- }
- /**
- * 列举空间,返回bucket列表
- *
- * @param string $region 区域
- * @param string $line
- * @param string $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
- * @return array
- */
- public function listbuckets(
- $region = null,
- $line = 'false',
- $shared = 'false'
- ) {
- $path = '/v3/buckets?region=' . $region . '&line=' . $line . '&shared=' . $shared;
- return $this->ucPost($path);
- }
- /**
- * 创建空间
- *
- * @param string $name 创建的空间名
- * @param string $region 创建的区域,默认华东
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/1382/mkbucketv3
- */
- public function createBucket($name, $region = 'z0')
- {
- $path = '/mkbucketv3/' . $name . '/region/' . $region;
- return $this->postV2($this->config->getUcHost() . $path, null);
- }
- /**
- * 删除空间
- *
- * @param string $name 需要删除的目标空间名
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/1601/drop-bucket
- */
- public function deleteBucket($name)
- {
- $path = '/drop/' . $name;
- return $this->postV2($this->config->getUcHost() . $path, null);
- }
- /**
- * 获取指定空间绑定的所有的域名
- *
- * @param string $bucket 空间名称
- * @return array
- */
- public function domains($bucket)
- {
- return $this->ucGet('/v2/domains?tbl=' . $bucket);
- }
- /**
- * 获取指定空间的相关信息
- *
- * @param string $bucket 空间名称
- * @return array
- */
- public function bucketInfo($bucket)
- {
- $path = '/v2/bucketInfo?bucket=' . $bucket;
- return $this->ucPost($path);
- }
- /**
- * 获取指定zone的空间信息列表
- *
- * @param string $region 区域
- * @param string $shared 指定共享空间,rw:读写权限空间,rd:读权限空间
- * @param string $fs 如果为 true,会返回每个空间当前的文件数和存储量(实时数据)
- * @return array
- */
- public function bucketInfos($region = null, $shared = 'false', $fs = 'false')
- {
- $path = '/v2/bucketInfos?region=' . $region . '&shared=' . $shared . '&fs=' . $fs;
- return $this->ucPost($path);
- }
- /**
- * 列取空间的文件列表
- *
- * @param string $bucket 空间名
- * @param string $prefix 列举前缀
- * @param string $marker 列举标识符
- * @param int $limit 单次列举个数限制
- * @param string $delimiter 指定目录分隔符
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/1284/list
- */
- public function listFiles(
- $bucket,
- $prefix = null,
- $marker = null,
- $limit = 1000,
- $delimiter = null
- ) {
- $query = array('bucket' => $bucket);
- \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
- \Qiniu\setWithoutEmpty($query, 'marker', $marker);
- \Qiniu\setWithoutEmpty($query, 'limit', $limit);
- \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
- return $this->rsfGet($bucket, '/list?' . http_build_query($query));
- }
- /**
- * 列取空间的文件列表
- *
- * @deprecated API 可能返回仅包含 marker,不包含 item 或 dir 的项,请使用 {@link listFiles}
- *
- * @param string $bucket 空间名
- * @param string $prefix 列举前缀
- * @param string $marker 列举标识符
- * @param int $limit 单次列举个数限制
- * @param string $delimiter 指定目录分隔符
- * @param bool $skipconfirm 是否跳过已删除条目的确认机制
- *
- * @return array
- * @link http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
- */
- public function listFilesv2(
- $bucket,
- $prefix = null,
- $marker = null,
- $limit = 1000,
- $delimiter = null,
- $skipconfirm = true
- ) {
- $query = array('bucket' => $bucket);
- \Qiniu\setWithoutEmpty($query, 'prefix', $prefix);
- \Qiniu\setWithoutEmpty($query, 'marker', $marker);
- \Qiniu\setWithoutEmpty($query, 'limit', $limit);
- \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter);
- \Qiniu\setWithoutEmpty($query, 'skipconfirm', $skipconfirm);
- $path = '/v2/list?' . http_build_query($query);
- list($host, $err) = $this->config->getRsfHostV2(
- $this->auth->getAccessKey(),
- $bucket,
- $this->proxy->makeReqOpt()
- );
- if ($err != null) {
- return array(null, $err);
- }
- $url = $host . $path;
- $headers = $this->auth->authorizationV2($url, 'POST', null, 'application/x-www-form-urlencoded');
- $ret = Client::post($url, null, $headers, $this->proxy->makeReqOpt());
- if (!$ret->ok()) {
- return array(null, new Error($url, $ret));
- }
- $r = explode("\n", $ret->body);
- array_pop($r);
- return array($r, null);
- }
- /**
- * 增加bucket生命规则
- *
- * @param string $bucket
- * 空间名
- * @param string $name
- * 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
- * @param string $prefix
- * 同一个 bucket 里面前缀不能重复
- * @param int $delete_after_days
- * 指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除。
- * 需大于 to_line_after_days
- * @param int $to_line_after_days
- * 指定文件上传多少天后转低频存储。指定为0表示不转低频存储
- * @param int $to_archive_ir_after_days
- * 指定文件上传多少天后转归档直读。指定为0表示不转归档直读
- * @param int $to_archive_after_days
- * 指定文件上传多少天后转归档存储。指定为0表示不转归档存储
- * @param int $to_deep_archive_after_days
- * 指定文件上传多少天后转深度归档存储。指定为0表示不转深度归档存储
- * @return array
- */
- public function bucketLifecycleRule(
- $bucket,
- $name,
- $prefix,
- $delete_after_days = null,
- $to_line_after_days = null,
- $to_archive_after_days = null,
- $to_deep_archive_after_days = null,
- $to_archive_ir_after_days = null
- ) {
- $path = '/rules/add';
- $params = array();
- if ($bucket) {
- $params['bucket'] = $bucket;
- }
- if ($name) {
- $params['name'] = $name;
- }
- if ($prefix) {
- $params['prefix'] = $prefix;
- }
- if ($delete_after_days) {
- $params['delete_after_days'] = $delete_after_days;
- }
- if ($to_line_after_days) {
- $params['to_line_after_days'] = $to_line_after_days;
- }
- if ($to_archive_ir_after_days) {
- $params['to_archive_ir_after_days'] = $to_archive_ir_after_days;
- }
- if ($to_archive_after_days) {
- $params['to_archive_after_days'] = $to_archive_after_days;
- }
- if ($to_deep_archive_after_days) {
- $params['to_deep_archive_after_days'] = $to_deep_archive_after_days;
- }
- $data = http_build_query($params);
- $info = $this->ucPost($path, $data);
- return $info;
- }
- /**
- * 更新bucket生命规则
- *
- * @param string $bucket
- * 空间名
- * @param string $name
- * 规则名称 bucket 内唯一,长度小于50,不能为空,只能为字母、数字、下划线
- * @param string $prefix
- * 同一个 bucket 里面前缀不能重复
- * @param int $delete_after_days
- * 指定上传文件多少天后删除,指定为0表示不删除,大于0表示多少天后删除
- * 需大于 to_line_after_days
- * @param int $to_line_after_days
- * 指定文件上传多少天后转低频存储。指定为0表示不转低频存储
- * @param int $to_archive_ir_after_days
- * 指定文件上传多少天后转归档只读。指定为0表示不转归档只读
- * @param int $to_archive_after_days
- * 指定文件上传多少天后转归档存储。指定为0表示不转归档存储
- * @param int $to_deep_archive_after_days
- * 指定文件上传多少天后转深度归档存储。指定为0表示不转深度归档存储
- * @return array
- */
- public function updateBucketLifecycleRule(
- $bucket,
- $name,
- $prefix,
- $delete_after_days = null,
- $to_line_after_days = null,
- $to_archive_after_days = null,
- $to_deep_archive_after_days = null,
- $to_archive_ir_after_days = null
- ) {
- $path = '/rules/update';
- $params = array();
- if ($bucket) {
- $params['bucket'] = $bucket;
- }
- if ($name) {
- $params['name'] = $name;
- }
- if ($prefix) {
- $params['prefix'] = $prefix;
- }
- if ($delete_after_days) {
- $params['delete_after_days'] = $delete_after_days;
- }
- if ($to_line_after_days) {
- $params['to_line_after_days'] = $to_line_after_days;
- }
- if ($to_archive_ir_after_days) {
- $params['to_archive_ir_after_days'] = $to_archive_ir_after_days;
- }
- if ($to_archive_after_days) {
- $params['to_archive_after_days'] = $to_archive_after_days;
- }
- if ($to_deep_archive_after_days) {
- $params['to_deep_archive_after_days'] = $to_deep_archive_after_days;
- }
- $data = http_build_query($params);
- return $this->ucPost($path, $data);
- }
- /**
- * 获取bucket生命规则
- *
- * @param string $bucket 空间名
- * @return array
- */
- public function getBucketLifecycleRules($bucket)
- {
- $path = '/rules/get?bucket=' . $bucket;
- $info = $this->ucGet($path);
- return $info;
- }
- /**
- * 删除bucket生命规则
- *
- * @param string $bucket 空间名
- * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
- * 只能为字母、数字、下划线()
- * @return array
- */
- public function deleteBucketLifecycleRule($bucket, $name)
- {
- $path = '/rules/delete';
- $params = array();
- if ($bucket) {
- $params['bucket'] = $bucket;
- }
- if ($name) {
- $params['name'] = $name;
- }
- $data = http_build_query($params);
- $info = $this->ucPost($path, $data);
- return $info;
- }
- /**
- * 增加bucket事件通知规则
- *
- * @param string $bucket 空间名
- * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
- * 只能为字母、数字、下划线()
- * @param string $prefix 同一个 bucket 里面前缀不能重复
- * @param string $suffix 可选,文件配置的后缀
- * @param array $event 事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,
- * disable,enable,deleteMarkerCreate
- * @param string $callbackURL 通知URL,可以指定多个,失败依次重试
- * @param string $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
- * @param string $host 可选,通知请求的host
- *
- * @return array
- */
- public function putBucketEvent(
- $bucket,
- $name,
- $prefix,
- $suffix,
- $event,
- $callbackURL,
- $access_key = null,
- $host = null
- ) {
- $path = '/events/add';
- $params = array();
- if (!empty($bucket)) {
- $params['bucket'] = $bucket;
- }
- if (!empty($name)) {
- $params['name'] = $name;
- }
- if (!empty($prefix)) {
- $params['prefix'] = $prefix;
- }
- if (!empty($suffix)) {
- $params['suffix'] = $suffix;
- }
- if (!empty($callbackURL)) {
- $params['callbackURL'] = $callbackURL;
- }
- if (!empty($access_key)) {
- $params['access_key'] = $access_key;
- }
- if (!empty($host)) {
- $params['host'] = $host;
- }
- $data = http_build_query($params);
- if (!empty($event)) {
- $eventpath = "";
- foreach ($event as $key => $value) {
- $eventpath .= "&event=$value";
- }
- $data .= $eventpath;
- }
- $info = $this->ucPost($path, $data);
- return $info;
- }
- /**
- * 更新bucket事件通知规则
- *
- * @param string $bucket 空间名
- * @param string $name 规则名称 bucket 内唯一,长度小于50,不能为空,
- * 只能为字母、数字、下划线()
- * @param string $prefix 同一个 bucket 里面前缀不能重复
- * @param string $suffix 可选,文件配置的后缀
- * @param array $event 事件类型,可以指定多个,包括 put,mkfile,delete,copy,move,append,disable,
- * enable,deleteMarkerCreate
- * @param string $callbackURL 通知URL,可以指定多个,失败依次重试
- * @param string $access_key 可选,设置的话会对通知请求用对应的ak、sk进行签名
- * @param string $host 可选,通知请求的host
- *
- * @return array
- */
- public function updateBucketEvent(
- $bucket,
- $name,
- $prefix,
- $suffix,
- $event,
- $callbackURL,
- $access_key = null,
- $host = null
- ) {
- $path = '/events/update';
- $params = array();
- if (!empty($bucket)) {
- $params['bucket'] = $bucket;
- }
- if (!empty($name)) {
- $params['name'] = $name;
- }
- if (!empty($prefix)) {
- $params['prefix'] = $prefix;
- }
- if ($suffix) {
- $params['suffix'] = $suffix;
- }
- if (!empty($event)) {
- $params['event'] = $event;
- }
- if (!empty($callbackURL)) {
- $params['callbackURL'] = $callbackURL;
- }
- if (!empty($access_key)) {
- $params['access_key'] = $access_key;
- }
- if (!empty($host)) {
- $params['host'] = $host;
- }
- $data = http_build_query($params);
- if (!empty($event)) {
- $eventpath = "";
- foreach ($event as $key => $value) {
- $eventpath .= "&event=$value";
- }
- $data .= $eventpath;
- }
- return $this->ucPost($path, $data);
- }
- /**
- * 获取bucket事件通知规则
- *
- * @param string $bucket 空间名
- * @return array
- */
- public function getBucketEvents($bucket)
- {
- $path = '/events/get?bucket=' . $bucket;
- return $this->ucGet($path);
- }
- /**
- * 删除bucket事件通知规则
- *
- * @param string $bucket 空间名
- * @param string $name 规则名称bucket内唯一,长度小于50,不能为空,只能为字母、数字、下划线
- * @return array
- */
- public function deleteBucketEvent($bucket, $name)
- {
- $path = '/events/delete';
- $params = array();
- if ($bucket) {
- $params['bucket'] = $bucket;
- }
- if ($name) {
- $params['name'] = $name;
- }
- $data = http_build_query($params);
- return $this->ucPost($path, $data);
- }
- /**
- * 获取bucket的跨域信息
- *
- * @param string $bucket 空间名
- * @return array
- */
- public function getCorsRules($bucket)
- {
- $path = '/corsRules/get/' . $bucket;
- return $this->ucGet($path);
- }
- /**
- * 开关原图保护
- *
- * @param string $bucket 空间名称
- * @param int $mode mode 为1表示开启原图保护,0表示关闭
- * @return array
- */
- public function putBucketAccessStyleMode($bucket, $mode)
- {
- $path = '/accessMode/' . $bucket . '/mode/' . $mode;
- return $this->ucPost($path, null);
- }
- /**
- * 设置私有属性
- *
- * @param string $bucket 空间名称
- * @param int $private private为0表示公开,为1表示私有
- * @return array
- */
- public function putBucketAccessMode($bucket, $private)
- {
- $path = "/private?bucket=$bucket&private=$private";
- return $this->ucPost($path, null);
- }
- /**
- * 设置 referer 防盗链
- *
- * @param string $bucket 空间名称
- * @param int $mode 0:关闭Referer(使用此选项将会忽略以下参数并将恢复默认值);
- * 1:设置Referer白名单; 2:设置Referer黑名单
- * @param string $norefer 0:不允许空 Refer 访问; 1:表示允许空Refer访问
- * @param string $pattern 规则字符串
- * @param int $enabled 源站是否支持,默认为0只给CDN配置, 设置为1表示开启源站防盗链
- * @return array
- * @link https://developer.qiniu.com/kodo/manual/6093/set-the-hotlinking-prevention
- */
- public function putReferAntiLeech($bucket, $mode, $norefer, $pattern, $enabled = 1)
- {
- $path = "/referAntiLeech?bucket=$bucket&mode=$mode&norefer=$norefer&pattern=$pattern&source_enabled=$enabled";
- return $this->ucPost($path, null);
- }
- /**
- * 设置Bucket的maxAge
- *
- * @param string $bucket 空间名称
- * @param int $maxAge maxAge为0或者负数表示为默认值(31536000)
- * @return array
- */
- public function putBucketMaxAge($bucket, $maxAge)
- {
- $path = '/maxAge?bucket=' . $bucket . '&maxAge=' . $maxAge;
- return $this->ucPost($path, null);
- }
- /**
- * 设置空间配额
- *
- * @param string $bucket 空间名称,不支持授权空间
- * @param string $size 空间存储量配额,参数传入0或不传表示不更改当前配置,传入-1表示取消限额,新创建的空间默认没有限额
- * @param string $count 空间文件数配额,参数含义同<size>
- * @return array
- */
- public function putBucketQuota($bucket, $size, $count)
- {
- $path = '/setbucketquota/' . $bucket . '/size/' . $size . '/count/' . $count;
- return $this->apiPost($bucket, $path);
- }
- /**
- * 获取空间配额
- *
- * @param string $bucket 空间名称
- * @return array
- */
- public function getBucketQuota($bucket)
- {
- $path = '/getbucketquota/' . $bucket;
- return $this->apiPost($bucket, $path);
- }
- /**
- * 获取资源的元信息,但不返回文件内容
- *
- * @param string $bucket 待获取信息资源所在的空间
- * @param string $key 待获取资源的文件名
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/1308/stat
- */
- public function stat($bucket, $key)
- {
- $path = '/stat/' . \Qiniu\entry($bucket, $key);
- return $this->rsGet($bucket, $path);
- }
- /**
- * 删除指定资源
- *
- * @param string $bucket 待删除资源所在的空间
- * @param string $key 待删除资源的文件名
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/1257/delete
- */
- public function delete($bucket, $key)
- {
- $path = '/delete/' . \Qiniu\entry($bucket, $key);
- return $this->rsPost($bucket, $path);
- }
- /**
- * 给资源进行重命名,本质为move操作。
- *
- * @param string $bucket 待操作资源所在空间
- * @param string $oldname 待操作资源文件名
- * @param string $newname 目标资源文件名
- *
- * @return array
- */
- public function rename($bucket, $oldname, $newname)
- {
- return $this->move($bucket, $oldname, $bucket, $newname);
- }
- /**
- * 对资源进行复制。
- *
- * @param string $from_bucket 待操作资源所在空间
- * @param string $from_key 待操作资源文件名
- * @param string $to_bucket 目标资源空间名
- * @param string $to_key 目标资源文件名
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/1254/copy
- */
- public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
- {
- $from = \Qiniu\entry($from_bucket, $from_key);
- $to = \Qiniu\entry($to_bucket, $to_key);
- $path = '/copy/' . $from . '/' . $to;
- if ($force === true) {
- $path .= '/force/true';
- }
- return $this->rsPost($from_bucket, $path);
- }
- /**
- * 将资源从一个空间到另一个空间
- *
- * @param string $from_bucket 待操作资源所在空间
- * @param string $from_key 待操作资源文件名
- * @param string $to_bucket 目标资源空间名
- * @param string $to_key 目标资源文件名
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/1288/move
- */
- public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = false)
- {
- $from = \Qiniu\entry($from_bucket, $from_key);
- $to = \Qiniu\entry($to_bucket, $to_key);
- $path = '/move/' . $from . '/' . $to;
- if ($force) {
- $path .= '/force/true';
- }
- return $this->rsPost($from_bucket, $path);
- }
- /**
- * 主动修改指定资源的文件元信息
- *
- * @param string $bucket 待操作资源所在空间
- * @param string $key 待操作资源文件名
- * @param string $mime 待操作文件目标mimeType
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/1252/chgm
- */
- public function changeMime($bucket, $key, $mime)
- {
- $resource = \Qiniu\entry($bucket, $key);
- $encode_mime = \Qiniu\base64_urlSafeEncode($mime);
- $path = '/chgm/' . $resource . '/mime/' . $encode_mime;
- return $this->rsPost($bucket, $path);
- }
- /**
- * 修改指定资源的存储类型
- *
- * @param string $bucket 待操作资源所在空间
- * @param string $key 待操作资源文件名
- * @param int $fileType 对象存储类型
- * 0 表示标准存储;
- * 1 表示低频存储;
- * 2 表示归档存储;
- * 3 表示深度归档存储;
- * 4 表示归档直读存储;
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/3710/chtype
- */
- public function changeType($bucket, $key, $fileType)
- {
- $resource = \Qiniu\entry($bucket, $key);
- $path = '/chtype/' . $resource . '/type/' . $fileType;
- return $this->rsPost($bucket, $path);
- }
- /**
- * 解冻指定资源的存储类型
- *
- * @param string $bucket 待操作资源所在空间
- * @param string $key 待操作资源文件名
- * @param int $freezeAfterDays 解冻有效时长,取值范围 1~7
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/6380/restore-archive
- */
- public function restoreAr($bucket, $key, $freezeAfterDays)
- {
- $resource = \Qiniu\entry($bucket, $key);
- $path = '/restoreAr/' . $resource . '/freezeAfterDays/' . $freezeAfterDays;
- return $this->rsPost($bucket, $path);
- }
- /**
- * 修改文件的存储状态,即禁用状态和启用状态间的的互相转换
- *
- * @param string $bucket 待操作资源所在空间
- * @param string $key 待操作资源文件名
- * @param int $status 0表示启用;1表示禁用
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/4173/modify-the-file-status
- */
- public function changeStatus($bucket, $key, $status)
- {
- $resource = \Qiniu\entry($bucket, $key);
- $path = '/chstatus/' . $resource . '/status/' . $status;
- return $this->rsPost($bucket, $path);
- }
- /**
- * 从指定URL抓取资源,并将该资源存储到指定空间中
- *
- * @param string $url 指定的URL
- * @param string $bucket 目标资源空间
- * @param string $key 目标资源文件名
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/1263/fetch
- */
- public function fetch($url, $bucket, $key = null)
- {
- $resource = \Qiniu\base64_urlSafeEncode($url);
- $to = \Qiniu\entry($bucket, $key);
- $path = '/fetch/' . $resource . '/to/' . $to;
- $ak = $this->auth->getAccessKey();
- list($ioHost, $err) = $this->config->getIovipHostV2($ak, $bucket, $this->proxy->makeReqOpt());
- if ($err != null) {
- return array(null, $err);
- }
- $url = $ioHost . $path;
- return $this->postV2($url, null);
- }
- /**
- * 从指定URL异步抓取资源,并将该资源存储到指定空间中
- *
- * @param string $url 需要抓取的url
- * @param string $bucket 所在区域的bucket
- * @param string $host 从指定url下载数据时使用的Host
- * @param string $key 文件存储的key
- * @param string $md5 文件md5
- * @param string $etag 文件etag
- * @param string $callbackurl 回调URL
- * @param string $callbackbody 回调Body
- * @param string $callbackbodytype 回调Body内容类型,默认为"application/x-www-form-urlencoded"
- * @param string $callbackhost 回调时使用的Host
- * @param int $file_type 存储文件类型
- * 0:标准存储(默认)
- * 1:低频存储
- * 2:归档存储
- * 3:深度归档存储
- * 4:归档直读存储
- * @param bool $ignore_same_key 如果空间中已经存在同名文件则放弃本次抓取
- * @return array
- * @link https://developer.qiniu.com/kodo/api/4097/asynch-fetch
- */
- public function asynchFetch(
- $url,
- $bucket,
- $host = null,
- $key = null,
- $md5 = null,
- $etag = null,
- $callbackurl = null,
- $callbackbody = null,
- $callbackbodytype = 'application/x-www-form-urlencoded',
- $callbackhost = null,
- $file_type = 0,
- $ignore_same_key = false
- ) {
- $path = '/sisyphus/fetch';
- $params = array('url' => $url, 'bucket' => $bucket);
- \Qiniu\setWithoutEmpty($params, 'host', $host);
- \Qiniu\setWithoutEmpty($params, 'key', $key);
- \Qiniu\setWithoutEmpty($params, 'md5', $md5);
- \Qiniu\setWithoutEmpty($params, 'etag', $etag);
- \Qiniu\setWithoutEmpty($params, 'callbackurl', $callbackurl);
- \Qiniu\setWithoutEmpty($params, 'callbackbody', $callbackbody);
- \Qiniu\setWithoutEmpty($params, 'callbackbodytype', $callbackbodytype);
- \Qiniu\setWithoutEmpty($params, 'callbackhost', $callbackhost);
- \Qiniu\setWithoutEmpty($params, 'file_type', $file_type);
- \Qiniu\setWithoutEmpty($params, 'ignore_same_key', $ignore_same_key);
- $data = json_encode($params);
- return $this->apiPost($bucket, $path, $data);
- }
- /**
- * 查询异步第三方资源抓取任务状态
- *
- * @param string $zone
- * @param string $id
- * @return array
- * @link https://developer.qiniu.com/kodo/api/4097/asynch-fetch
- */
- public function asynchFetchStatus($zone, $id)
- {
- $scheme = "http://";
- if ($this->config->useHTTPS === true) {
- $scheme = "https://";
- }
- $url = $scheme . "api-" . $zone . ".qiniuapi.com/sisyphus/fetch?id=" . $id;
- list($ret, $err) = $this->getV2($url);
- if ($err != null) {
- return array(null, $err);
- }
- return array($ret, null);
- }
- /**
- * 从镜像源站抓取资源到空间中,如果空间中已经存在,则覆盖该资源
- *
- * @param string $bucket 待获取资源所在的空间
- * @param string $key 代获取资源文件名
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/1293/prefetch
- */
- public function prefetch($bucket, $key)
- {
- $resource = \Qiniu\entry($bucket, $key);
- $path = '/prefetch/' . $resource;
- $ak = $this->auth->getAccessKey();
- list($ioHost, $err) = $this->config->getIovipHostV2($ak, $bucket, $this->proxy->makeReqOpt());
- if ($err != null) {
- return array(null, $err);
- }
- $url = $ioHost . $path;
- return $this->postV2($url, null);
- }
- /**
- * 在单次请求中进行多个资源管理操作
- *
- * @param array $operations 资源管理操作数组
- *
- * @return array 每个资源的处理情况,结果类似:
- * [
- * { "code" => <HttpCode int>, "data" => <Data> },
- * { "code" => <HttpCode int> },
- * { "code" => <HttpCode int> },
- * { "code" => <HttpCode int> },
- * { "code" => <HttpCode int>, "data" => { "error": "<ErrorMessage string>" } },
- * ...
- * ]
- * @link http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html
- */
- public function batch($operations)
- {
- $scheme = "http://";
- if ($this->config->useHTTPS === true) {
- $scheme = "https://";
- }
- $params = 'op=' . implode('&op=', $operations);
- $errResp = new Response(0, 0);
- if (count($operations) <= 0) {
- $errResp->error = 'empty operations';
- return array(null, new Error($scheme . '/batch', $errResp));
- }
- $bucket = '';
- foreach ($operations as $op) {
- $segments = explode('/', $op);
- if (count($segments) < 3) {
- continue;
- }
- list($bucket,) = \Qiniu\decodeEntry($segments[2]);
- }
- return $this->rsPost($bucket, '/batch', $params);
- }
- /**
- * 设置文件的生命周期
- *
- * @param string $bucket 设置文件生命周期文件所在的空间
- * @param string $key 设置文件生命周期文件的文件名
- * @param int $days 设置该文件多少天后删除,当$days设置为0时表示取消该文件的生命周期
- *
- * @return array
- * @link https://developer.qiniu.com/kodo/api/update-file-lifecycle
- */
- public function deleteAfterDays($bucket, $key, $days)
- {
- $entry = \Qiniu\entry($bucket, $key);
- $path = "/deleteAfterDays/$entry/$days";
- return $this->rsPost($bucket, $path);
- }
- /**
- * 更新 object 生命周期
- *
- * @param string $bucket 空间名
- * @param string $key 目标资源
- * @param int $to_line_after_days 多少天后将文件转为低频存储。
- * -1 表示取消已设置的转低频存储的生命周期规则;
- * 0 表示不修改转低频生命周期规则。
- * @param int $to_archive_ir_after_days 多少天后转为归档直读存储。
- * -1 表示取消已设置的转归档直读存储的生命周期规则;
- * 0 表示不修改转归档直读生命周期规则。
- * @param int $to_archive_after_days 多少天后将文件转为归档存储。
- * -1 表示取消已设置的转归档存储的生命周期规则;
- * 0 表示不修改转归档生命周期规则。
- * @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
- * -1 表示取消已设置的转深度归档存储的生命周期规则;
- * 0 表示不修改转深度归档生命周期规则。
- * @param int $delete_after_days 多少天后将文件删除。
- * -1 表示取消已设置的删除存储的生命周期规则;
- * 0 表示不修改删除存储的生命周期规则。
- * @return array
- */
- public function setObjectLifecycle(
- $bucket,
- $key,
- $to_line_after_days = 0,
- $to_archive_after_days = 0,
- $to_deep_archive_after_days = 0,
- $delete_after_days = 0,
- $to_archive_ir_after_days = 0
- ) {
- return $this->setObjectLifecycleWithCond(
- $bucket,
- $key,
- null,
- $to_line_after_days,
- $to_archive_after_days,
- $to_deep_archive_after_days,
- $delete_after_days,
- $to_archive_ir_after_days
- );
- }
- /**
- * 更新 object 生命周期
- *
- * @param string $bucket 空间名
- * @param string $key 目标资源
- * @param int $to_line_after_days 多少天后将文件转为低频存储。
- * 设置为 -1 表示取消已设置的转低频存储的生命周期规则;
- * 0 表示不修改转低频生命周期规则。
- * @param int $to_archive_ir_after_days 多少天后将文件转为归档直读存储。
- * 设置为 -1 表示取消已设置的转归档直读存储的生命周期规则;
- * 0 表示不修改转归档直读生命周期规则。
- * @param int $to_archive_after_days 多少天后将文件转为归档存储。
- * -1 表示取消已设置的转归档存储的生命周期规则;
- * 0 表示不修改转归档生命周期规则。
- * @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
- * -1 表示取消已设置的转深度归档存储的生命周期规则;
- * 0 表示不修改转深度归档生命周期规则。
- * @param int $delete_after_days 多少天后将文件删除。
- * -1 表示取消已设置的删除存储的生命周期规则;
- * 0 表示不修改删除存储的生命周期规则。
- * @param array<string, mixed> $cond 匹配条件,只有条件匹配才会设置成功。
- * 目前支持:hash、mime、fsize、putTime
- * @return array
- */
- public function setObjectLifecycleWithCond(
- $bucket,
- $key,
- $cond = null,
- $to_line_after_days = 0,
- $to_archive_after_days = 0,
- $to_deep_archive_after_days = 0,
- $delete_after_days = 0,
- $to_archive_ir_after_days = 0
- ) {
- $encodedEntry = \Qiniu\entry($bucket, $key);
- $path = '/lifecycle/' . $encodedEntry .
- '/toIAAfterDays/' . $to_line_after_days .
- '/toArchiveIRAfterDays/' . $to_archive_ir_after_days .
- '/toArchiveAfterDays/' . $to_archive_after_days .
- '/toDeepArchiveAfterDays/' . $to_deep_archive_after_days .
- '/deleteAfterDays/' . $delete_after_days;
- if ($cond != null) {
- $condStrArr = array();
- foreach ($cond as $key => $value) {
- array_push($condStrArr, $key . '=' . $value);
- }
- $condStr = implode('&', $condStrArr);
- $path .= '/cond' . \Qiniu\base64_urlSafeEncode($condStr);
- }
- return $this->rsPost($bucket, $path);
- }
- private function rsfGet($bucket, $path)
- {
- list($host, $err) = $this->config->getRsfHostV2(
- $this->auth->getAccessKey(),
- $bucket,
- $this->proxy->makeReqOpt()
- );
- if ($err != null) {
- return array(null, $err);
- }
- return $this->getV2($host . $path);
- }
- private function rsGet($bucket, $path)
- {
- list($host, $err) = $this->config->getRsHostV2(
- $this->auth->getAccessKey(),
- $bucket,
- $this->proxy->makeReqOpt()
- );
- if ($err != null) {
- return array(null, $err);
- }
- return $this->getV2($host . $path);
- }
- private function rsPost($bucket, $path, $body = null)
- {
- list($host, $err) = $this->config->getRsHostV2(
- $this->auth->getAccessKey(),
- $bucket,
- $this->proxy->makeReqOpt()
- );
- if ($err != null) {
- return array(null, $err);
- }
- return $this->postV2($host . $path, $body);
- }
- private function apiGet($bucket, $path)
- {
- list($host, $err) = $this->config->getApiHostV2(
- $this->auth->getAccessKey(),
- $bucket,
- $this->proxy->makeReqOpt()
- );
- if ($err != null) {
- return array(null, $err);
- }
- return $this->getV2($host . $path);
- }
- private function apiPost($bucket, $path, $body = null)
- {
- list($host, $err) = $this->config->getApiHostV2(
- $this->auth->getAccessKey(),
- $bucket,
- $this->proxy->makeReqOpt()
- );
- if ($err != null) {
- return array(null, $err);
- }
- return $this->postV2($host . $path, $body);
- }
- private function ucGet($path)
- {
- $url = $this->config->getUcHost() . $path;
- return $this->getV2($url);
- }
- private function ucPost($path, $body = null)
- {
- $url = $this->config->getUcHost() . $path;
- return $this->postV2($url, $body);
- }
- private function getV2($url)
- {
- $headers = $this->auth->authorizationV2($url, 'GET', null, 'application/x-www-form-urlencoded');
- $ret = Client::get($url, $headers, $this->proxy->makeReqOpt());
- if (!$ret->ok()) {
- return array(null, new Error($url, $ret));
- }
- return array($ret->json(), null);
- }
- private function postV2($url, $body)
- {
- $headers = $this->auth->authorizationV2($url, 'POST', $body, 'application/x-www-form-urlencoded');
- $ret = Client::post($url, $body, $headers, $this->proxy->makeReqOpt());
- if (!$ret->ok()) {
- return array(null, new Error($url, $ret));
- }
- $r = ($ret->body === null) ? array() : $ret->json();
- return array($r, null);
- }
- public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket, $force)
- {
- return self::twoKeyBatch('/copy', $source_bucket, $key_pairs, $target_bucket, $force);
- }
- public static function buildBatchRename($bucket, $key_pairs, $force)
- {
- return self::buildBatchMove($bucket, $key_pairs, $bucket, $force);
- }
- public static function buildBatchMove($source_bucket, $key_pairs, $target_bucket, $force)
- {
- return self::twoKeyBatch('/move', $source_bucket, $key_pairs, $target_bucket, $force);
- }
- public static function buildBatchDelete($bucket, $keys)
- {
- return self::oneKeyBatch('/delete', $bucket, $keys);
- }
- public static function buildBatchStat($bucket, $keys)
- {
- return self::oneKeyBatch('/stat', $bucket, $keys);
- }
- public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
- {
- $data = array();
- foreach ($key_day_pairs as $key => $day) {
- array_push($data, '/deleteAfterDays/' . \Qiniu\entry($bucket, $key) . '/' . $day);
- }
- return $data;
- }
- /**
- * @param string $bucket 空间名
- * @param array<string> $keys 目标资源
- * @param int $to_line_after_days 多少天后将文件转为低频存储。
- * -1 表示取消已设置的转低频存储的生命周期规则;
- * 0 表示不修改转低频生命周期规则。
- * @param int $to_archive_ir_after_days 多少天后将文件转为归档直读。
- * -1 表示取消已设置的转归档只读的生命周期规则;
- * 0 表示不修改转归档只读周期规则。
- * @param int $to_archive_after_days 多少天后将文件转为归档存储。
- * -1 表示取消已设置的转归档存储的生命周期规则;
- * 0 表示不修改转归档生命周期规则。
- * @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
- * -1 表示取消已设置的转深度归档存储的生命周期规则;
- * 0 表示不修改转深度归档生命周期规则。
- * @param int $delete_after_days 多少天后将文件删除。
- * -1 表示取消已设置的删除存储的生命周期规则;
- * 0 表示不修改删除存储的生命周期规则。
- *
- * @retrun array<string>
- */
- public static function buildBatchSetObjectLifecycle(
- $bucket,
- $keys,
- $to_line_after_days,
- $to_archive_after_days,
- $to_deep_archive_after_days,
- $delete_after_days,
- $to_archive_ir_after_days = 0
- ) {
- $result = array();
- foreach ($keys as $key) {
- $encodedEntry = \Qiniu\entry($bucket, $key);
- $op = '/lifecycle/' . $encodedEntry .
- '/toIAAfterDays/' . $to_line_after_days .
- '/toArchiveIRAfterDays/' . $to_archive_ir_after_days .
- '/toArchiveAfterDays/' . $to_archive_after_days .
- '/toDeepArchiveAfterDays/' . $to_deep_archive_after_days .
- '/deleteAfterDays/' . $delete_after_days;
- array_push($result, $op);
- }
- return $result;
- }
- public static function buildBatchChangeMime($bucket, $key_mime_pairs)
- {
- $data = array();
- foreach ($key_mime_pairs as $key => $mime) {
- array_push($data, '/chgm/' . \Qiniu\entry($bucket, $key) . '/mime/' . base64_encode($mime));
- }
- return $data;
- }
- public static function buildBatchChangeType($bucket, $key_type_pairs)
- {
- $data = array();
- foreach ($key_type_pairs as $key => $type) {
- array_push($data, '/chtype/' . \Qiniu\entry($bucket, $key) . '/type/' . $type);
- }
- return $data;
- }
- public static function buildBatchRestoreAr($bucket, $key_restore_days_pairs)
- {
- $data = array();
- foreach ($key_restore_days_pairs as $key => $restore_days) {
- array_push($data, '/restoreAr/' . \Qiniu\entry($bucket, $key) . '/freezeAfterDays/' . $restore_days);
- }
- return $data;
- }
- private static function oneKeyBatch($operation, $bucket, $keys)
- {
- $data = array();
- foreach ($keys as $key) {
- array_push($data, $operation . '/' . \Qiniu\entry($bucket, $key));
- }
- return $data;
- }
- private static function twoKeyBatch($operation, $source_bucket, $key_pairs, $target_bucket, $force)
- {
- if ($target_bucket === null) {
- $target_bucket = $source_bucket;
- }
- $data = array();
- $forceOp = "false";
- if ($force) {
- $forceOp = "true";
- }
- foreach ($key_pairs as $from_key => $to_key) {
- $from = \Qiniu\entry($source_bucket, $from_key);
- $to = \Qiniu\entry($target_bucket, $to_key);
- array_push($data, $operation . '/' . $from . '/' . $to . "/force/" . $forceOp);
- }
- return $data;
- }
- }
|