Maison > Article > développement back-end > 七牛云存储官方接口PHP版本_PHP教程
此 SDK 适用于 PHP 5.1.0 及其以上版本。基于 七牛云存储官方API 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个网站程序,还是包括从云端(服务端程序)到终端(手持设备应用)的架构的服务或应用,通过七牛云存储及其 SDK,都能让您应用程序的终端用户高速上传和下载,同时也让您的服务端更加轻盈。
SDK源码地址:https://github.com/qiniu/php-sdk/tags
要接入七牛云存储,您需要拥有一对有效的 Access Key 和 Secret Key 用来进行签名认证。可以通过如下步骤获得:
示例代码如下:
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="variable">$bucket</span> = <span class="string">"phpsdk"</span>; <span class="variable">$key</span> = <span class="string">"pic.jpg"</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="variable">$client</span> = <span class="keyword">new</span> Qiniu_MacHttpClient(<span class="keyword">null</span>); <span class="keyword">list</span>(<span class="variable">$ret</span>, <span class="variable">$err</span>) = Qiniu_RS_Stat(<span class="variable">$client</span>, <span class="variable">$bucket</span>, <span class="variable">$key</span>); <span class="keyword">echo</span> <span class="string">"Qiniu_RS_Stat result: \n"</span>; <span class="keyword">if</span> (<span class="variable">$err</span> !== <span class="keyword">null</span>) { var_dump(<span class="variable">$err</span>); } <span class="keyword">else</span> { var_dump(<span class="variable">$ret</span>); } </code>
示例代码如下:
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="variable">$bucket</span> = <span class="string">"phpsdk"</span>; <span class="variable">$key</span> = <span class="string">"pic.jpg"</span>; <span class="variable">$key1</span> = <span class="string">"file_name1"</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="variable">$client</span> = <span class="keyword">new</span> Qiniu_MacHttpClient(<span class="keyword">null</span>); <span class="variable">$err</span> = Qiniu_RS_Copy(<span class="variable">$client</span>, <span class="variable">$bucket</span>, <span class="variable">$key</span>, <span class="variable">$bucket</span>, <span class="variable">$key1</span>); <span class="keyword">echo</span> <span class="string">"====> Qiniu_RS_Copy result: \n"</span>; <span class="keyword">if</span> (<span class="variable">$err</span> !== <span class="keyword">null</span>) { var_dump(<span class="variable">$err</span>); } <span class="keyword">else</span> { <span class="keyword">echo</span> <span class="string">"Success!"</span>; } </code>
示例代码如下:
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="variable">$bucket</span> = <span class="string">"phpsdk"</span>; <span class="variable">$key</span> = <span class="string">"pic.jpg"</span>; <span class="variable">$key1</span> = <span class="string">"file_name1"</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="variable">$client</span> = <span class="keyword">new</span> Qiniu_MacHttpClient(<span class="keyword">null</span>); <span class="variable">$err</span> = Qiniu_RS_Move(<span class="variable">$client</span>, <span class="variable">$bucket</span>, <span class="variable">$key</span>, <span class="variable">$bucket</span>, <span class="variable">$key1</span>); <span class="keyword">echo</span> <span class="string">"====> Qiniu_RS_Move result: \n"</span>; <span class="keyword">if</span> (<span class="variable">$err</span> !== <span class="keyword">null</span>) { var_dump(<span class="variable">$err</span>); } <span class="keyword">else</span> { <span class="keyword">echo</span> <span class="string">"Success!"</span>; } </code>
示例代码如下:
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="variable">$bucket</span> = <span class="string">"phpsdk"</span>; <span class="variable">$key1</span> = <span class="string">"file_name1"</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="variable">$client</span> = <span class="keyword">new</span> Qiniu_MacHttpClient(<span class="keyword">null</span>); <span class="variable">$err</span> = Qiniu_RS_Delete(<span class="variable">$client</span>, <span class="variable">$bucket</span>, <span class="variable">$key1</span>); <span class="keyword">echo</span> <span class="string">"====> Qiniu_RS_Delete result: \n"</span>; <span class="keyword">if</span> (<span class="variable">$err</span> !== <span class="keyword">null</span>) { var_dump(<span class="variable">$err</span>); } <span class="keyword">else</span> { <span class="keyword">echo</span> <span class="string">"Success!"</span>; } </code>
###1. 文件上传
为了尽可能地改善终端用户的上传体验,七牛云存储首创了客户端直传功能。一般云存储的上传流程是:
<code class="php">客户端(终端用户) => 业务服务器 => 云存储服务 </code>
这样多了一次上传的流程,和本地存储相比,会相对慢一些。但七牛引入了客户端直传,将整个上传过程调整为:
<code class="php">客户端(终端用户) => 七牛 => 业务服务器 </code>
客户端(终端用户)直接上传到七牛的服务器,通过DNS智能解析,七牛会选择到离终端用户最近的ISP服务商节点,速度会比本地存储快很多。文件上传成功以后,七牛的服务器使用回调功能,只需要将非常少的数据(比如Key)传给应用服务器,应用服务器进行保存即可。
在七牛云存储中,整个上传流程大体分为这样几步:
需要注意的是,回调到业务服务器的过程是可选的,它取决于业务服务器颁发的 uptoken。如果没有回调,七牛会返回一些标准的信息(比如文件的 hash)给客户端。如果上传发生在业务服务器,以上流程可以自然简化为:
服务端生成 uptoken 代码如下:
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="variable">$bucket</span> = <span class="string">'phpsdk'</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="variable">$putPolicy</span> = <span class="keyword">new</span> Qiniu_RS_PutPolicy(<span class="variable">$bucket</span>); <span class="variable">$upToken</span> = <span class="variable">$putPolicy</span>->Token(<span class="keyword">null</span>); </code>
上传文件到七牛(通常是客户端完成,但也可以发生在服务端):
上传字符串
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/io.php"</span>); <span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="variable">$bucket</span> = <span class="string">"phpsdk"</span>; <span class="variable">$key1</span> = <span class="string">"file_name1"</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="variable">$putPolicy</span> = <span class="keyword">new</span> Qiniu_RS_PutPolicy(<span class="variable">$bucket</span>); <span class="variable">$upToken</span> = <span class="variable">$putPolicy</span>->Token(<span class="keyword">null</span>); <span class="keyword">list</span>(<span class="variable">$ret</span>, <span class="variable">$err</span>) = Qiniu_Put(<span class="variable">$upToken</span>, <span class="variable">$key1</span>, <span class="string">"Qiniu Storage!"</span>, <span class="keyword">null</span>); <span class="keyword">echo</span> <span class="string">"====> Qiniu_Put result: \n"</span>; <span class="keyword">if</span> (<span class="variable">$err</span> !== <span class="keyword">null</span>) { var_dump(<span class="variable">$err</span>); } <span class="keyword">else</span> { var_dump(<span class="variable">$ret</span>); } </code>
上传本地文件
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/io.php"</span>); <span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="variable">$bucket</span> = <span class="string">"phpsdk"</span>; <span class="variable">$key1</span> = <span class="string">"file_name1"</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="variable">$putPolicy</span> = <span class="keyword">new</span> Qiniu_RS_PutPolicy(<span class="variable">$bucket</span>); <span class="variable">$upToken</span> = <span class="variable">$putPolicy</span>->Token(<span class="keyword">null</span>); <span class="variable">$putExtra</span> = <span class="keyword">new</span> Qiniu_PutExtra(); <span class="variable">$putExtra</span>->Crc32 = <span class="number">1</span>; <span class="keyword">list</span>(<span class="variable">$ret</span>, <span class="variable">$err</span>) = Qiniu_PutFile(<span class="variable">$upToken</span>, <span class="variable">$key1</span>, __file__, <span class="variable">$putExtra</span>); <span class="keyword">echo</span> <span class="string">"====> Qiniu_PutFile result: \n"</span>; <span class="keyword">if</span> (<span class="variable">$err</span> !== <span class="keyword">null</span>) { var_dump(<span class="variable">$err</span>); } <span class="keyword">else</span> { var_dump(<span class="variable">$ret</span>); } </code>
uptoken 实际上是用 AccessKey/SecretKey 进行数字签名的上传策略(Qiniu_RS_PutPolicy
),它控制则整个上传流程的行为。让我们快速过一遍你都能够决策啥:
<code class="php"><span class="class"><span class="keyword">class</span> <span class="title">Qiniu_RS_PutPolicy</span> {</span> <span class="keyword">public</span> <span class="variable">$Scope</span>; <span class="comment">// 必选项。可以是 bucketName 或者 bucketName:key</span> <span class="keyword">public</span> <span class="variable">$CallbackUrl</span>; <span class="comment">// 可选</span> <span class="keyword">public</span> <span class="variable">$CallbackBody</span>; <span class="comment">// 可选</span> <span class="keyword">public</span> <span class="variable">$ReturnUrl</span>; <span class="comment">// 可选, 更贴切的名字是 redirectUrl。</span> <span class="keyword">public</span> <span class="variable">$ReturnBody</span>; <span class="comment">// 可选</span> <span class="keyword">public</span> <span class="variable">$AsyncOps</span>; <span class="comment">// 可选</span> <span class="keyword">public</span> <span class="variable">$EndUser</span>; <span class="comment">// 可选</span> <span class="keyword">public</span> <span class="variable">$Expires</span>; <span class="comment">// 可选。默认是 3600 秒</span> } </code>
scope
限定客户端的权限。如果 scope
是 bucket,则客户端只能新增文件到指定的 bucket,不能修改文件。如果 scope
为 bucket:key,则客户端可以修改指定的文件。callbackUrl
设定业务服务器的回调地址,这样业务服务器才能感知到上传行为的发生。callbackBody
设定业务服务器的回调信息。文件上传成功后,七牛向业务服务器的callbackUrl发送的POST请求携带的数据。支持 魔法变量 和 自定义变量。returnUrl
设置用于浏览器端文件上传成功后,浏览器执行301跳转的URL,一般为 HTML Form 上传时使用。文件上传成功后浏览器会自动跳转到 returnUrl?upload_ret=returnBody
。returnBody
可调整返回给客户端的数据包,支持 魔法变量 和 自定义变量。returnBody
只在没有 callbackUrl
时有效(否则直接返回 callbackUrl
返回的结果)。不同情形下默认返回的 returnBody
并不相同。在一般情况下返回的是文件内容的 hash
,也就是下载该文件时的 etag
;但指定 returnUrl
时默认的 returnBody
会带上更多的信息。asyncOps
可指定上传完成后,需要自动执行哪些数据处理。这是因为有些数据处理操作(比如音视频转码)比较慢,如果不进行预转可能第一次访问的时候效果不理想,预转可以很大程度改善这一点。关于上传策略更完整的说明,请参考 uptoken。
七牛云存储上的资源下载分为 公有资源下载 和 私有资源下载 。
私有(private)是 Bucket(空间)的一个属性,一个私有 Bucket 中的资源为私有资源,私有资源不可匿名下载。
新创建的空间(Bucket)缺省为私有,也可以将某个 Bucket 设为公有,公有 Bucket 中的资源为公有资源,公有资源可以匿名下载。
如果在给bucket绑定了域名的话,可以通过以下地址访问。
<code class="xml">[GET] http://<span class="tag"><<span class="title">domain</span>></span>/<span class="tag"><<span class="title">key</span>></span> </code>
示例代码:
<code class="ruby"><span class="variable">$key</span> = <span class="string">'pic.jpg'</span>; <span class="variable">$domain</span> = <span class="string">'phpsdk.qiniudn.com'</span>; <span class="regexp">//</span><span class="variable">$baseUrl</span> 就是您要访问资源的地址 <span class="variable">$baseUrl</span> = <span class="constant">Qiniu_RS_MakeBaseUrl</span>(<span class="variable">$domain</span>, <span class="variable">$key</span>); </code>
其中可以到七牛云存储开发者自助网站绑定, 域名可以使用自己一级域名的或者是由七牛提供的二级域名(<bucket>.qiniudn.com</bucket>
)。注意,尖括号不是必需,代表替换项。
私有资源必须通过临时下载授权凭证(downloadToken)下载,如下:
<code class="xml">[GET] http://<span class="tag"><<span class="title">domain</span>></span>/<span class="tag"><<span class="title">key</span>></span>?e=<span class="tag"><<span class="title">deadline</span>></span>&token=<span class="tag"><<span class="title">downloadToken</span>></span> </code>
注意,尖括号不是必需,代表替换项。
私有下载链接可以使用 SDK 提供的如下方法生成:
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="variable">$key</span> = <span class="string">'pic.jpg'</span>; <span class="variable">$domain</span> = <span class="string">'phpsdk.qiniudn.com'</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="variable">$baseUrl</span> = Qiniu_RS_MakeBaseUrl(<span class="variable">$domain</span>, <span class="variable">$key</span>); <span class="variable">$getPolicy</span> = <span class="keyword">new</span> Qiniu_RS_GetPolicy(); <span class="variable">$privateUrl</span> = <span class="variable">$getPolicy</span>->MakeRequest(<span class="variable">$baseUrl</span>, <span class="keyword">null</span>); <span class="keyword">echo</span> <span class="string">"====> getPolicy result: \n"</span>; <span class="keyword">echo</span> <span class="variable">$privateUrl</span> . <span class="string">"\n"</span>; </code>
七牛支持在云端对图像, 视频, 音频等富媒体进行个性化处理
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="keyword">require_once</span>(<span class="string">"qiniu/fop.php"</span>); <span class="variable">$key</span> = <span class="string">'pic.jpg'</span>; <span class="variable">$domain</span> = <span class="string">'phpsdk.qiniudn.com'</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="comment">//生成baseUrl</span> <span class="variable">$baseUrl</span> = Qiniu_RS_MakeBaseUrl(<span class="variable">$domain</span>, <span class="variable">$key</span>); <span class="comment">//生成fopUrl</span> <span class="variable">$imgInfo</span> = <span class="keyword">new</span> Qiniu_ImageInfo; <span class="variable">$imgInfoUrl</span> = <span class="variable">$imgInfo</span>->MakeRequest(<span class="variable">$baseUrl</span>); <span class="comment">//对fopUrl 进行签名,生成privateUrl。 公有bucket 此步可以省去。</span> <span class="variable">$getPolicy</span> = <span class="keyword">new</span> Qiniu_RS_GetPolicy(); <span class="variable">$imgInfoPrivateUrl</span> = <span class="variable">$getPolicy</span>->MakeRequest(<span class="variable">$imgInfoUrl</span>, <span class="keyword">null</span>); <span class="keyword">echo</span> <span class="string">"====> imageInfo privateUrl: \n"</span>; <span class="keyword">echo</span> <span class="variable">$imgInfoPrivateUrl</span> . <span class="string">"\n"</span>; </code>
将$imgInfoPrivateUrl
粘贴到浏览器地址栏中就可以查看该图像的信息了。
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="keyword">require_once</span>(<span class="string">"qiniu/fop.php"</span>); <span class="variable">$key</span> = <span class="string">'pic.jpg'</span>; <span class="variable">$domain</span> = <span class="string">'phpsdk.qiniudn.com'</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="comment">//生成baseUrl</span> <span class="variable">$baseUrl</span> = Qiniu_RS_MakeBaseUrl(<span class="variable">$domain</span>, <span class="variable">$key</span>); <span class="comment">//生成fopUrl</span> <span class="variable">$imgExif</span> = <span class="keyword">new</span> Qiniu_Exif; <span class="variable">$imgExifUrl</span> = <span class="variable">$imgExif</span>->MakeRequest(<span class="variable">$baseUrl</span>); <span class="comment">//对fopUrl 进行签名,生成privateUrl。 公有bucket 此步可以省去。</span> <span class="variable">$getPolicy</span> = <span class="keyword">new</span> Qiniu_RS_GetPolicy(); <span class="variable">$imgExifPrivateUrl</span> = <span class="variable">$getPolicy</span>->MakeRequest(<span class="variable">$imgExifUrl</span>, <span class="keyword">null</span>); <span class="keyword">echo</span> <span class="string">"====> imageView privateUrl: \n"</span>; <span class="keyword">echo</span> <span class="variable">$imgExifPrivateUrl</span> . <span class="string">"\n"</span>; </code>
<code class="php"><span class="keyword">require_once</span>(<span class="string">"qiniu/rs.php"</span>); <span class="keyword">require_once</span>(<span class="string">"qiniu/fop.php"</span>); <span class="variable">$key</span> = <span class="string">'pic.jpg'</span>; <span class="variable">$domain</span> = <span class="string">'phpsdk.qiniudn.com'</span>; <span class="variable">$accessKey</span> = <span class="string">'<YOUR_APP_ACCESS_KEY>'</span>; <span class="variable">$secretKey</span> = <span class="string">'<YOUR_APP_SECRET_KEY>'</span>; Qiniu_SetKeys(<span class="variable">$accessKey</span>, <span class="variable">$secretKey</span>); <span class="comment">//生成baseUrl</span> <span class="variable">$baseUrl</span> = Qiniu_RS_MakeBaseUrl(<span class="variable">$domain</span>, <span class="variable">$key</span>); <span class="comment">//生成fopUrl</span> <span class="variable">$imgView</span> = <span class="keyword">new</span> Qiniu_ImageView; <span class="variable">$imgView</span>->Mode = <span class="number">1</span>; <span class="variable">$imgView</span>->Width = <span class="number">60</span>; <span class="variable">$imgView</span>->Height = <span class="number">120</span>; <span class="variable">$imgViewUrl</span> = <span class="variable">$imgView</span>->MakeRequest(<span class="variable">$baseUrl</span>); <span class="comment">//对fopUrl 进行签名,生成privateUrl。 公有bucket 此步可以省去。</span> <span class="variable">$getPolicy</span> = <span class="keyword">new</span> Qiniu_RS_GetPolicy(); <span class="variable">$imgViewPrivateUrl</span> = <span class="variable">$getPolicy</span>->MakeRequest(<span class="variable">$imgViewUrl</span>, <span class="keyword">null</span>); <span class="keyword">echo</span> <span class="string">"====> imageView privateUrl: \n"</span>; <span class="keyword">echo</span> <span class="variable">$imgViewPrivateUrl</span> . <span class="string">"\n"</span>; </code>
Copyright © 2013 qiniu.com
基于 MIT 协议发布:
七牛云存储提供的免费额度:
存储空间10GB
每月 PUT/DELETE 10万次请求
每月下载流量10GB
每月 GET 100万次请求
欢迎关注方倍工作室微信,了解我们及行业的最新动态