具体可以参考七牛PHP SDK的源码以及官网使用文档:
https://github.com/qiniu/php-sdk
http://developer.qiniu.com/docs/v6/sdk/php-sdk.html
文件上传
上传代码
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require_once</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'path_to_sdk/vendor/autoload.php'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 引入鉴权类</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 引入上传类</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Storage</span>\<span class="hljs-title" style="box-sizing: border-box;">UploadManager</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 需要填写你的 Access Key 和 Secret Key</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 构建鉴权对象</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 要上传的空间</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Bucket_Name'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 生成上传 Token</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$token</span> = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>->uploadToken(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 要上传文件的本地路径</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$filePath</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'./php-logo.png'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 上传到七牛后保存的文件名</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'my-php-logo.png'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化 UploadManager 对象并进行文件的上传。</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$uploadMgr</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> UploadManager(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 调用 UploadManager 的 putFile 方法进行文件的上传。</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">list</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>) = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$uploadMgr</span>->putFile(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$token</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$filePath</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n====> putFile result: \n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> !== <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li></ul>
上传&回调
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require_once</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'path_to_sdk/vendor/autoload.php'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Storage</span>\<span class="hljs-title" style="box-sizing: border-box;">UploadManager</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Bucket_Name'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 上传文件到七牛后, 七牛将文件名和文件大小回调给业务服务器.</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$policy</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">array</span>( <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'callbackUrl'</span> => <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://your.domain.com/callback.php'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'callbackBody'</span> => <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'filename=$(fname)&filesize=$(fsize)'</span> ); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$uptoken</span> = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>->uploadToken(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3600</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$policy</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//上传文件的本地路径</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$filePath</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'./php-logo.png'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$uploadMgr</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> UploadManager(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">list</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>) = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$uploadMgr</span>->putFile(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$uptoken</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$filePath</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n====> putFile result: \n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> !== <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li></ul>
上传&预转持续化(以视频转码为例)
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require_once</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'path_to_sdk/vendor/autoload.php'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Storage</span>\<span class="hljs-title" style="box-sizing: border-box;">UploadManager</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Bucket_Name'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//转码是使用的队列名称。 </span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$pipeline</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'abc'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//要进行转码的转码操作。 </span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"avthumb/mp4/s/640x360/vb/1.25m"</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//可以对转码后的文件进行使用saveas参数自定义命名,当然也可以不指定文件会默认命名并保存在当间。</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$savekey</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'目标Bucket_Name:自定义文件key'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span>.<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'|saveas/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$savekey</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$policy</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">array</span>( <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'persistentOps'</span> => <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'persistentPipeline'</span> => <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$pipeline</span> ); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$uptoken</span> = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>->uploadToken(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3600</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$policy</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//上传文件的本地路径</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$filePath</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'./php-logo.png'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$uploadMgr</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> UploadManager(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">list</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>) = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$uploadMgr</span>->putFile(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$uptoken</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$filePath</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n====> putFile result: \n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> !== <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>); } 注:上面的Demo只是针对视频转码,如果需要别的功能比如音视频切片、视频截图、视频拼接只需要修改下上面的fops后面的参数就可以了, eg: <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"vframe/jpg/offset/1/w/480/h/360/rotate/90"</span>就表示视频截图了。 下面给出一些常见的数据处理功能,可以根据需要进行选择: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------图片缩放-------------------</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> =<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'imageView/2/w/200/h/200'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------视频转码-------------------</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// $fops ='avthumb/flv/vb/229k/vcodec/libx264/noDomain/1';</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------图片水印-------------------</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$base64URL</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://developer.qiniu.com/resource/logo-2.jpg'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'watermark/1/image/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$base64URL</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------视频切片-------------------</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'avthumb/m3u8/pattern/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$savets</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//切片与加密参数</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'avthumb/m3u8/vb/640k/hlsKey/MDEyMzQ1Njc4OTEyMzQ1Ng==/hlsKeyUrl/aHR0cDovLzd4bGVrYi5jb20yLnowLmdsYi5xaW5pdWNkbi5jb20vcWluaXV0ZXN0LmtleQ=='</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------文档转换-------------------</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'yifangyun_preview'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------视频截图-------------------</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'vframe/jpg/offset/1/w/480/h/360/rotate/90'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------视频拼接-------------------</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//拼接视频片段时要保证所有源的画面长宽值一致</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//除去作为数据处理对象的源文件以外,还可以指定最多5个源文件(即总计6个片段)</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//所有源文件必须属于同一存储空间</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//格式:avconcat/<Mode>/format/<Format>/<encodedUrl0>/<encodedUrl1>/<encodedUrl2>/...</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedUrl1</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://7xl4c9.com1.z0.glb.clouddn.com/pingjie2.flv'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedUrl2</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://7xl4c9.com1.z0.glb.clouddn.com/pingjie3.avi'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'avconcat/2/format/mp4/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedUrl1</span>.<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedUrl2</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------多文件压缩-------------------</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//可将若干七牛空间中的资源文件,在七牛服务端压缩后存储</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//格式:mkzip/<mode>/url/<Base64EncodedURL>/alias/<Base64EncodedAlias>/url/<Base64EncodedURL></span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile1</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://7xl4c9.com1.z0.glb.clouddn.com/photo1.jpg'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile2</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://7xl4c9.com1.z0.glb.clouddn.com/vedio1.mp4'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile3</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://7xl4c9.com1.z0.glb.clouddn.com/audio1.mp3'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mkzip/2/url/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile1</span>.<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'/url/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile2</span>.<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'/url/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile3</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li></ul>
可以看到上传成功后的回调行为主要是有上传 Token
中的 put policy
来指定。其中put policy可以指定的行为不止这些,具体可以参考签权类中的policy字段:
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$policyFields</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">array</span>( <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'callbackUrl'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'callbackBody'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'callbackHost'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'callbackBodyType'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'callbackFetchKey'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'returnUrl'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'returnBody'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'endUser'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'saveKey'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'insertOnly'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'detectMime'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mimeLimit'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'fsizeLimit'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'persistentOps'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'persistentNotifyUrl'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'persistentPipeline'</span>, );</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li></ul>
下面我们给一个例子来看怎么接收回调信息,并验证该回调是否来自七牛:
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require_once</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'path_to_sdk/vendor/autoload.php'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取回调的body信息</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$callbackBody</span> = file_get_contents(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'php://input'</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//回调的contentType</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$contentType</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'application/x-www-form-urlencoded'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//回调的签名信息,可以验证该回调是否来自七牛</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$authorization</span> = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$_SERVER</span>[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'HTTP_AUTHORIZATION'</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//七牛回调的url,具体可以参考。</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$url</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://your.domain.com/callback.php'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$isQiniuCallback</span> = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>->verifyCallback(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$contentType</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$authorization</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$url</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$callbackBody</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$isQiniuCallback</span>) { <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$resp</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">array</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'ret'</span> => <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'success'</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$resp</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">array</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'ret'</span> => <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'failed'</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> json_encode(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$resp</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li></ul>
文件下载
下载代码
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'path_to_sdk/vendor/autoload.php'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//baseUrl构造成私有空间的域名/key的形式</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$baseUrl</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://domain/key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$authUrl</span> = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>->privateDownloadUrl(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$baseUrl</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$authUrl</span>; <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">?></span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>
空间资源的管理
获取文件的信息
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require_once</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'path_to_sdk/vendor/autoload.php'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Storage</span>\<span class="hljs-title" style="box-sizing: border-box;">BucketManager</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化Auth状态:</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化BucketManager</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucketMgr</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> BucketManager(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//你要测试的空间, 并且这个key在你空间中存在</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Bucket_Name'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'php-logo.png'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取文件的状态信息</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">list</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>) = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucketMgr</span>->stat(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n====> $key stat : \n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> !== <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul>
移动单个文件
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require_once</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'path_to_sdk/vendor/autoload.php'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Storage</span>\<span class="hljs-title" style="box-sizing: border-box;">BucketManager</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化Auth状态:</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化BucketManager</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucketMgr</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> BucketManager(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//你要测试的空间, 并且这个key在你空间中存在</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Bucket_Name'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'php-logo.png'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取文件的状态信息</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">list</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>) = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucketMgr</span>->stat(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n====> $key stat : \n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> !== <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul>
复制单个文件
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require_once</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'path_to_sdk/vendor/autoload.php'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Storage</span>\<span class="hljs-title" style="box-sizing: border-box;">BucketManager</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化Auth状态:</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化BucketManager</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucketMgr</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> BucketManager(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//你要测试的空间, 并且这个key在你空间中存在</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Bucket_Name'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'php-logo.png'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//将文件从文件$key 复制到文件$key2。 可以在不同bucket复制</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key2</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'php-logo2.png'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucketMgr</span>->copy(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key2</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n====> copy $key to $key2 : \n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> !== <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Success!"</span>; } </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul>
删除单个文件
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require_once</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'path_to_sdk/vendor/autoload.php'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Storage</span>\<span class="hljs-title" style="box-sizing: border-box;">BucketManager</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化Auth状态:</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化BucketManager</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucketMgr</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> BucketManager(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//你要测试的空间, 并且这个key在你空间中存在</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Bucket_Name'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'php-logo.png'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//删除$bucket 中的文件 $key</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucketMgr</span>->delete(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n====> delete $key : \n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> !== <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Success!"</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul>
列举空间中文件
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require_once</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">__DIR__</span> . <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'/../autoload.php'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Storage</span>\<span class="hljs-title" style="box-sizing: border-box;">BucketManager</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucketMgr</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> BucketManager(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 要列取的空间名称</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Bucket_Name'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 要列取文件的公共前缀</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$prefix</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">''</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$marker</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">''</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$limit</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">list</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$iterms</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$marker</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>) = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucketMgr</span>->listFiles(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$prefix</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$marker</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$limit</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> !== <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n====> list file err: \n"</span>; var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Marker: $marker\n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\nList Iterms====>\n"</span>; var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$iterms</span>); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul>
触发持久化操作(以视频转码为例)
视频转码
<code class="language-php hljs has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"><?php</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">require_once</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'path_to_sdk/vendor/autoload.php'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Auth</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Qiniu</span>\<span class="hljs-title" style="box-sizing: border-box;">Processing</span>\<span class="hljs-title" style="box-sizing: border-box;">PersistentFop</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//对已经上传到七牛的视频发起异步转码操作 </span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Access_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Secret_Key'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Auth(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$accessKey</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$secretKey</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//要转码的文件所在的空间和文件名。</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Bucket_Name'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'1.mp4'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//转码是使用的队列名称。 </span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$pipeline</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'abc'</span>; <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$pfop</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> PersistentFop(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$auth</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$bucket</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$pipeline</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//要进行转码的转码操作。 </span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"avthumb/mp4/s/640x360/vb/1.25m"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">list</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$id</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>) = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$pfop</span>->execute(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$key</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n====> pfop avthumb result: \n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"PersistentFop Id: $id\n"</span>; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//查询转码的进度和状态</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">list</span>(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>, <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>) = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$pfop</span>->status(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$id</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\n====> pfop avthumb status: \n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span> != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$err</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { var_dump(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ret</span>); } 注:同样,上面的Demo只是针对视频转码,如果需要别的功能比如音视频切片、视频截图、视频拼接只需要修改下上面的fops后面的参数就可以了, eg: <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"vframe/jpg/offset/1/w/480/h/360/rotate/90"</span>就表示视频截图了。 下面给出一些常见的数据处理功能,可以根据需要进行选择: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------图片缩放-------------------</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> =<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'imageView/2/w/200/h/200'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------视频转码-------------------</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// $fops ='avthumb/flv/vb/229k/vcodec/libx264/noDomain/1';</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------图片水印-------------------</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$base64URL</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://developer.qiniu.com/resource/logo-2.jpg'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'watermark/1/image/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$base64URL</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------视频切片-------------------</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'avthumb/m3u8/pattern/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$savets</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//切片与加密参数</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'avthumb/m3u8/vb/640k/hlsKey/MDEyMzQ1Njc4OTEyMzQ1Ng==/hlsKeyUrl/aHR0cDovLzd4bGVrYi5jb20yLnowLmdsYi5xaW5pdWNkbi5jb20vcWluaXV0ZXN0LmtleQ=='</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------文档转换-------------------</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'yifangyun_preview'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------视频截图-------------------</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'vframe/jpg/offset/1/w/480/h/360/rotate/90'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------视频拼接-------------------</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//拼接视频片段时要保证所有源的画面长宽值一致</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//除去作为数据处理对象的源文件以外,还可以指定最多5个源文件(即总计6个片段)</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//所有源文件必须属于同一存储空间</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//格式:avconcat/<Mode>/format/<Format>/<encodedUrl0>/<encodedUrl1>/<encodedUrl2>/...</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedUrl1</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://7xl4c9.com1.z0.glb.clouddn.com/pingjie2.flv'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedUrl2</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://7xl4c9.com1.z0.glb.clouddn.com/pingjie3.avi'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'avconcat/2/format/mp4/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedUrl1</span>.<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedUrl2</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//------------------多文件压缩-------------------</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//可将若干七牛空间中的资源文件,在七牛服务端压缩后存储</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//格式:mkzip/<mode>/url/<Base64EncodedURL>/alias/<Base64EncodedAlias>/url/<Base64EncodedURL></span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile1</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://7xl4c9.com1.z0.glb.clouddn.com/photo1.jpg'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile2</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://7xl4c9.com1.z0.glb.clouddn.com/vedio1.mp4'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile3</span> = Qiniu\base64_urlSafeEncode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://7xl4c9.com1.z0.glb.clouddn.com/audio1.mp3'</span>); <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$fops</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'mkzip/2/url/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile1</span>.<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'/url/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile2</span>.<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'/url/'</span>.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$encodedfile3</span>;</code>