Home >Backend Development >PHP Tutorial >Qiniu Cloud Storage official interface PHP version_PHP tutorial
This SDK is suitable for PHP 5.1.0 and above. Built based on the official API of Qiniu Cloud Storage. Using this SDK to build your web application allows you to securely store data on Qiniu Cloud Storage in a very convenient way. Whether your network application is a website program, or a service or application that includes an architecture from the cloud (server program) to the terminal (handheld device application), Qiniu Cloud Storage and its SDK can make the terminal of your application Users can upload and download at high speed, while also making your server lighter.
SDK source code address: https://github.com/qiniu/php-sdk/tags
To access Qiniu Cloud Storage, you need to have a pair of valid Access Key and Secret Key for signature authentication. It can be obtained through the following steps:
The sample code is as follows:
<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>
The sample code is as follows:
<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>
The sample code is as follows:
<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>
The sample code is as follows:
<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. File upload
In order to improve the uploading experience of end users as much as possible, Qiniu Cloud Storage pioneered the client direct upload function. The general cloud storage upload process is:
<code class="php">客户端(终端用户) => 业务服务器 => 云存储服务 </code>
This adds an additional upload process, which will be relatively slower than local storage. However, Qiniu introduced client-side direct transmission and adjusted the entire upload process to:
<code class="php">客户端(终端用户) => 七牛 => 业务服务器 </code>
The client (end user) directly uploads to Qiniu's server. Through intelligent DNS resolution, Qiniu will choose the ISP service provider node closest to the end user, which will be much faster than local storage. After the file is uploaded successfully, Qiniu's server uses the callback function and only needs to pass very small data (such as Key) to the application server, and the application server saves it.
In Qiniu Cloud Storage, the entire upload process is roughly divided into the following steps:
It should be noted that the process of callback to the business server is optional and depends on the uptoken issued by the business server. If there is no callback, Qiniu will return some standard information (such as the hash of the file) to the client. If the upload occurs on the business server, the above process can be naturally simplified to:
The server-side generated uptoken code is as follows:
<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>
Upload files to Qiniu (usually done on the client side, but can also happen on the server side):
Upload string
<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>
Upload local files
<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 is actually a digitally signed upload strategy (Qiniu_RS_PutPolicy
) using AccessKey/SecretKey, which controls the behavior of the entire upload process. Let’s quickly go over what you can decide:
<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>
其中可以到七牛云存储开发者自助网站绑定, 域名可以使用自己一级域名的或者是由七牛提供的二级域名(280134e6c3237783be1035f17b978582.qiniudn.com
)。注意,尖括号不是必需,代表替换项。
私有资源必须通过临时下载授权凭证(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万次请求
欢迎关注方倍工作室微信,了解我们及行业的最新动态