Compare commits

...

23 Commits

Author SHA1 Message Date
ufo2243
ab79c6b1ae
Update README.md 2023-06-21 09:38:18 +08:00
ufo2243
793a4fef43
Update README.md 2023-06-19 15:49:10 +08:00
ufo2243
332770e79f
Create LICENSE 2023-06-19 15:47:59 +08:00
ufo2243
8c125d0213
Merge pull request #6 from ufilesdk-dev/feat/5-http-timeout
http.php curl timeout changed to 20s
2022-09-13 09:42:05 +08:00
ufo2243
4779049fae
Merge pull request #7 from ufilesdk-dev/del_bom_header
删除文件中的BOM 头
2022-08-30 11:16:43 +08:00
patrick
8498d2f80b 删除文件中的BOM 头 2022-08-29 21:31:35 +08:00
XuSenqi
fb8cda063c
Merge pull request #4 from ufilesdk-dev/from_pxu_urlencode
UCloud_ListObjects urlencode query
2022-08-24 17:35:14 +08:00
patrick.xu
418725fb99 http.php curl timeout changed to 20s 2022-08-23 14:50:31 +08:00
patrick.xu
9af6f6af5d UCloud_ListObjects urlencode query 2022-04-12 21:09:38 +08:00
delex.xie
a5ef7e0ccc 更新文件编码方式 2020-09-16 13:59:05 +08:00
delex.xie
c8d6006822
Update mimetypes.php 2020-07-15 16:01:07 +08:00
delex.xie
1daed4cf76 新增head接口 2020-07-07 20:00:42 +08:00
delex.xie
0e4135d715
Update README.md 2020-05-18 16:14:12 +08:00
delex.xie
0c2a88d040 新增listobjects接口 2020-05-18 16:11:31 +08:00
delex.xie
12af6b3e3b
修改ipa 的文件类型 2020-04-10 11:43:21 +08:00
delex.xie
2af55e185a
Update http.php
优化返回值兼容Etag
2020-03-27 19:49:42 +08:00
delex.xie
14093f24bf
Update mimetypes.php
新增:".svg" => "image/svg+xml",
2019-07-11 16:44:13 +08:00
delexxie
d6ffe9118e
Update README.md 2019-04-10 19:46:55 +08:00
delexxie
88e302dd72
Update README.md 2019-04-10 19:46:40 +08:00
delexxie
18317d89cd
Update README.md 2019-04-10 19:46:25 +08:00
delexxie
4f0837d83e
Update README.md 2019-04-10 19:43:42 +08:00
delexxie
7d21ebb8ec
Update README.md 2019-04-10 19:42:20 +08:00
delexxie
738a9ac389
Create README.md 2019-04-10 19:41:51 +08:00
10 changed files with 766 additions and 349 deletions

201
LICENSE Normal file
View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright UCloud
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

29
README.md Normal file
View File

@ -0,0 +1,29 @@
# UCloud 对象存储官方 Php 语言 SDK
[![Software License](https://img.shields.io/github/license/saltstack/salt)](LICENSE)
## US3 (原名UFile) 对象存储基本概念
在对象存储系统中存储空间Bucket是文件File的组织管理单位文件File是存储空间的逻辑存储单元。对于每个账号该账号里存放的每个文件都有唯一的一对存储空间Bucket与键Key作为标识。我们可以把 Bucket 理解成一类文件的集合Key 理解成文件名。由于每个 Bucket 需要配置和权限不同,每个账户里面会有多个 Bucket。在 US3 里面Bucket 主要分为公有和私有两种,公有 Bucket 里面的文件可以对任何人开放,私有 Bucket 需要配置对应访问签名才能访问。
使用本 SDK 你不需要考虑签名,包装 URL处理 HTTP response code 等一系列非常繁琐的事情。
## 功能说明
ucloud/conf.php 为配置文件,按需填写:
- $UCLOUD_PROXY_SUFFIX = '.cn-bj.ufileos.com';
- $UCLOUD_PUBLIC_KEY = 'paste your public key here';
- $UCLOUD_PRIVATE_KEY = 'paste your private key here';
Demo 目录中,包含各个接口的使用例子:
- append.php
- delete.php
- get.php
- multipart.php
- mupload.php
- put.php
- listobjects.php
- uploadhit.php
## 许可证
[Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html)

53
v1/del_bom_header.php Normal file
View File

@ -0,0 +1,53 @@
<?php
if (isset($_GET['dir'])) { //设置文件目录
$basedir = $_GET['dir'];
}else{
$basedir = '.';
}
$auto = true; //定义是否去掉文件中的BOM头如果为 false 则只检测是否含有 BOM 头
checkdir($basedir);//检测目录
function checkdir($basedir){
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if($file{0} == '.'){
continue;
}
if($file != '.' && $file != '..'){
if (!is_dir($basedir."/".$file)) {
echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>";
}else{
$dirname = $basedir."/".$file;
checkdir($dirname);
}
}
}
closedir($dh);
}
}
//检查文件是否有BOM头通过 全局变量 $auto 来控制是否删除文件中的BOM头
function checkBOM ($filename) {
global $auto;
$contents = file_get_contents($filename);
$charset[1] = substr($contents, 0, 1);
$charset[2] = substr($contents, 1, 1);
$charset[3] = substr($contents, 2, 1);
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
if ($auto) {
$rest = substr($contents, 3);
rewrite ($filename, $rest);
return ("<font color=red>BOM found, automatically removed.</font>");
} else {
return ("<font color=red>BOM found.</font>");
}
}else{
return ("BOM Not Found.");
}
}
//重写文件以达到删除BOM头的目的
function rewrite ($filename, $data) {
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>

21
v1/demo/head.php Normal file
View File

@ -0,0 +1,21 @@
<?php
require_once("../ucloud/proxy.php");
//存储空间名
$bucket = "your bucket";
//上传至存储空间后的文件名称(请不要和API公私钥混淆)
$key = "your key";
list($header, $data, $err) = UCloud_Head($bucket, $key);
if ($err) {
echo "error: " . $err->ErrMsg . "\n";
echo "code: " . $err->Code . "\n";
exit;
}else{
echo "code: " . 200 ."\n";
}
print_r($header);
echo "head $bucket/$key success\n";

26
v1/demo/listobjects.php Normal file
View File

@ -0,0 +1,26 @@
<?php
require_once("../ucloud/proxy.php");
//存储空间名
$bucket = "your bucket";
//需要拉列表的目录前缀
$prefix = "path prefix";
//拉列表一般分页拉取每次拉取会返回一个marker用来作为下一次拉取的marker实现翻页拉取初始为""
//如果拉取到结尾:"IsTruncated":false,"NextMarker":""
$marker = "";
#默认分隔符,目前支持/ 和空字符;如果是空表示不区分目录递归返回列表;
$delimiter ="/";
//该接口拉取一个目录前缀下的目录、文件列表
list($data, $err) = UCloud_ListObjects($bucket, $prefix, $marker, 100, $delimiter);
if ($err) {
echo "error: " . $err->ErrMsg . "\n";
echo "code: " . $err->Code . "\n";
exit;
}
echo json_encode($data, 128);

View File

@ -5,7 +5,7 @@ global $UCLOUD_PROXY_SUFFIX;
global $UCLOUD_PUBLIC_KEY;
global $UCLOUD_PRIVATE_KEY;
$SDK_VER = "1.0.8";
$SDK_VER = "1.0.9";
//空间域名后缀,请查看控制台上空间域名再配置此处
//https://docs.ucloud.cn/storage_cdn/ufile/tools/introduction

View File

@ -4,8 +4,8 @@ require_once("conf.php");
require_once("utils.php");
require_once("digest.php");
$CURL_TIMEOUT=30;
// --------------------------------------------------------------------------------
class HTTP_Request
{
public $URL;
@ -36,7 +36,7 @@ class HTTP_Request
global $UFILE_ACTION_TYPE;
if ($CURL_TIMEOUT == null && $action_type !== ActionType::PUTFILE
&& $action_type !== ActionType::POSTFILE) {
$CURL_TIMEOUT = 10;
$CURL_TIMEOUT = 20;
}
$this->Timeout = $CURL_TIMEOUT;
}
@ -70,7 +70,7 @@ class HTTP_Response
global $CURL_TIMEOUT;
if ($CURL_TIMEOUT == null) {
$CURL_TIMEOUT = 10;
$CURL_TIMEOUT = 20;
}
$this->Timeout = $CURL_TIMEOUT;
}
@ -129,13 +129,16 @@ function UCloud_Client_Do($req)
CURLOPT_TIMEOUT => $req->Timeout,
CURLOPT_CONNECTTIMEOUT => $req->Timeout
);
if($req->EncodedQuery() !== ""){
if($req->METHOD =="HEAD"){
$options[CURLOPT_NOBODY]=true;
}
if($req->EncodedQuery() !== ""){
$options[CURLOPT_URL] = $url['host'] . "/" . $url['path'] . "?" . $req->EncodedQuery();
}else{
$options[CURLOPT_URL] = $url['host'] . "/" . $url['path'];
}
$httpHeader = $req->Header;
if (!empty($httpHeader))
{
@ -151,7 +154,9 @@ function UCloud_Client_Do($req)
} else {
$options[CURLOPT_POSTFIELDS] = "";
}
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$ret = curl_errno($ch);
if ($ret !== 0) {
@ -159,6 +164,7 @@ function UCloud_Client_Do($req)
curl_close($ch);
return array(null, $err);
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
@ -253,7 +259,12 @@ function UCloud_Client_Ret($resp)
}
$etag = UCloud_Header_Get($resp->Header, 'ETag');
if ($etag != '') $data['ETag'] = $etag;
if ($etag != ''){
$data['ETag'] = $etag;
}else{
$data['ETag'] = UCloud_Header_Get($resp->Header, 'Etag');
}
if (floor($code/100) == 2) {
return array($data, null);
}
@ -270,6 +281,33 @@ function UCloud_Client_Call($self, $req, $type = HEAD_FIELD_CHECK)
return UCloud_Client_Ret($resp);
}
//@results: ($header,$data, $error)
function UCloud_Client_Call_ReHeader($self, $req, $type = HEAD_FIELD_CHECK)
{
list($resp, $err) = $self->RoundTrip($req, $type);
if ($err !== null) {
return array(null, null, $err);
}
$code = $resp->StatusCode;
$data = null;
if ($code >= 200 && $code <= 299) {
if ($resp->ContentLength !== 0 && UCloud_Header_Get($resp->Header, 'Content-Type') == 'application/json') {
$data = json_decode($resp->Body, true);
if ($data === null) {
$err = new UCloud_Error($code, 0, "");
return array($resp->Header, null, $err);
}
}
}
if (floor($code/100) == 2) {
return array($resp->Header, $data, null);
}
return array($resp->Header, $data, UCloud_ResponseError($resp));
}
//@results: $error
function UCloud_Client_CallNoRet($self, $req, $type = HEAD_FIELD_CHECK)
{

View File

@ -260,6 +260,7 @@ $mimetype_complete_map = array(
".ppm" => "image/x-portable-pixmap",
".pps" => "application/mspowerpoint",
".ppt" => "application/mspowerpoint",
".pptx" => "application/mspowerpoint",
".ppz" => "application/mspowerpoint",
".pre" => "application/x-freelance",
".prt" => "application/pro_eng",
@ -337,6 +338,7 @@ $mimetype_complete_map = array(
".svf" => "image/vnd.dwg",
".svr" => "application/x-world",
".swf" => "application/x-shockwave-flash",
".svg" => "image/svg+xml",
".t" => "application/x-troff",
".talk" => "text/x-speech",
".tar" => "application/x-tar",
@ -438,7 +440,7 @@ $mimetype_complete_map = array(
".zoo" => "application/octet-stream",
".zsh" => "text/x-script.zsh",
".apk" => "application/vnd.android.package-archive",
".ipa" => "application/vnd.android.package-archive",
".ipa" => "application/octet-stream.ipa",
".flv" => "video/x-flv",
".mp4" => "video/mp4",
".m3u8" => "application/x-mpegURL",

View File

@ -1,341 +1,384 @@
<?php
require_once("conf.php");
require_once("http.php");
require_once("utils.php");
require_once("digest.php");
//------------------------------普通上传------------------------------
function UCloud_PutFile($bucket, $key, $file)
{
$action_type = ActionType::PUTFILE;
$err = CheckConfig(ActionType::PUTFILE);
if ($err != null) {
return array(null, $err);
}
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = $key;
$content = @fread($f, filesize($file));
list($mimetype, $err) = GetFileMimeType($file);
if ($err) {
fclose($f);
return array("", $err);
}
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
$req->Header['Expect'] = '';
$req->Header['Content-Type'] = $mimetype;
$client = new UCloud_AuthHttpClient(null, $mimetype);
list($data, $err) = UCloud_Client_Call($client, $req);
fclose($f);
return array($data, $err);
}
//------------------------------表单上传------------------------------
function UCloud_MultipartForm($bucket, $key, $file)
{
$action_type = ActionType::POSTFILE;
$err = CheckConfig(ActionType::POSTFILE);
if ($err != null) {
return array(null, $err);
}
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = "";
$fsize = filesize($file);
$content = "";
if ($fsize != 0) {
$content = @fread($f, filesize($file));
if ($content == FALSE) {
fclose($f);
return array(null, new UCloud_Error(0, -1, "read file error"));
}
}
list($mimetype, $err) = GetFileMimeType($file);
if ($err) {
fclose($f);
return array("", $err);
}
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
$req->Header['Expect'] = '';
$token = UCloud_SignRequest(null, $req, $mimetype);
$fields = array('Authorization'=>$token, 'FileName' => $key);
$files = array('files'=>array('file', $file, $content, $mimetype));
$client = new UCloud_AuthHttpClient(null, NO_AUTH_CHECK);
list($data, $err) = UCloud_Client_CallWithMultipartForm($client, $req, $fields, $files);
fclose($f);
return array($data, $err);
}
//------------------------------分片上传------------------------------
function UCloud_MInit($bucket, $key)
{
$err = CheckConfig(ActionType::MINIT);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = $key;
$querys = array(
"uploads" => ""
);
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call($client, $req);
}
//@results: (tagList, err)
function UCloud_MUpload($bucket, $key, $file, $uploadId, $blkSize, $partNumber=0)
{
$err = CheckConfig(ActionType::MUPLOAD);
if ($err != null) {
return array(null, $err);
}
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$etagList = array();
list($mimetype, $err) = GetFileMimeType($file);
if ($err) {
fclose($f);
return array("", $err);
}
$client = new UCloud_AuthHttpClient(null);
for(;;) {
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = $key;
if (@fseek($f, $blkSize*$partNumber, SEEK_SET) < 0) {
fclose($f);
return array(null, new UCloud_Error(0, -1, "fseek error"));
}
$content = @fread($f, $blkSize);
if ($content == FALSE) {
if (feof($f)) break;
fclose($f);
return array(null, new UCloud_Error(0, -1, "read file error"));
}
$querys = array(
"uploadId" => $uploadId,
"partNumber" => $partNumber
);
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path, 'query'=>$querys), $content, $bucket, $key);
$req->Header['Content-Type'] = $mimetype;
$req->Header['Expect'] = '';
list($data, $err) = UCloud_Client_Call($client, $req);
if ($err) {
fclose($f);
return array(null, $err);
}
$etag = @$data['ETag'];
$part = @$data['PartNumber'];
if ($part != $partNumber) {
fclose($f);
return array(null, new UCloud_Error(0, -1, "unmatch partnumber"));
}
$etagList[] = $etag;
$partNumber += 1;
}
fclose($f);
return array($etagList, null);
}
function UCloud_MFinish($bucket, $key, $uploadId, $etagList, $newKey = '')
{
$err = CheckConfig(ActionType::MFINISH);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = $key;
$querys = array(
'uploadId' => $uploadId,
'newKey' => $newKey,
);
$body = @implode(',', $etagList);
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), $body, $bucket, $key);
$req->Header['Content-Type'] = 'text/plain';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call($client, $req);
}
function UCloud_MCancel($bucket, $key, $uploadId)
{
$err = CheckConfig(ActionType::MCANCEL);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = $key;
$querys = array(
'uploadId' => $uploadId
);
$req = new HTTP_Request('DELETE', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call($client, $req);
}
//------------------------------秒传------------------------------
function UCloud_UploadHit($bucket, $key, $file)
{
$err = CheckConfig(ActionType::UPLOADHIT);
if ($err != null) {
return array(null, $err);
}
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
$content = "";
$fileSize = filesize($file);
if ($fileSize != 0) {
$content = @fread($f, $fileSize);
if ($content == FALSE) {
fclose($f);
return array(null, new UCloud_Error(0, -1, "read file error"));
}
}
list($fileHash, $err) = UCloud_FileHash($file);
if ($err) {
fclose($f);
return array(null, $err);
}
fclose($f);
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = "uploadhit";
$querys = array(
'Hash' => $fileHash,
'FileName' => $key,
'FileSize' => $fileSize
);
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call($client, $req);
}
//------------------------------删除文件------------------------------
function UCloud_Delete($bucket, $key)
{
$err = CheckConfig(ActionType::DELETE);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = "$key";
$req = new HTTP_Request('DELETE', array('host'=>$host, 'path'=>$path), null, $bucket, $key);
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call($client, $req);
}
//------------------------------追加上传------------------------------
function UCloud_AppendFile($bucket, $key, $file, $position)
{
$action_type = ActionType::APPENDFILE;
$err = CheckConfig(ActionType::APPENDFILE);
if ($err != null) {
return array(null, $err);
}
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$key = $key . "?append&position=" . $position;
$path = $key;
$content = @fread($f, filesize($file));
list($mimetype, $err) = GetFileMimeType($file);
if ($err) {
fclose($f);
return array("", $err);
}
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
$req->Header['Expect'] = '';
$req->Header['Content-Type'] = $mimetype;
$client = new UCloud_AuthHttpClient(null, $mimetype);
list($data, $err) = UCloud_Client_Call($client, $req);
fclose($f);
return array($data, $err);
}
//------------------------------生成公有文件Url------------------------------
// @results: $url
function UCloud_MakePublicUrl($bucket, $key)
{
global $UCLOUD_PROXY_SUFFIX;
return $bucket . $UCLOUD_PROXY_SUFFIX . "/" . rawurlencode($key);
}
//------------------------------生成私有文件Url------------------------------
// @results: $url
function UCloud_MakePrivateUrl($bucket, $key, $expires = 0)
{
$err = CheckConfig(ActionType::GETFILE);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PUBLIC_KEY;
$public_url = UCloud_MakePublicUrl($bucket, $key);
$req = new HTTP_Request('GET', array('path'=>$public_url), null, $bucket, $key);
if ($expires > 0) {
$req->Header['Expires'] = $expires;
}
$client = new UCloud_AuthHttpClient(null);
$temp = $client->Auth->SignRequest($req, null, QUERY_STRING_CHECK);
$signature = substr($temp, -28, 28);
$url = $public_url . "?UCloudPublicKey=" . rawurlencode($UCLOUD_PUBLIC_KEY) . "&Signature=" . rawurlencode($signature);
if ('' != $expires) {
$url .= "&Expires=" . rawurlencode($expires);
}
return $url;
}
<?php
require_once("conf.php");
require_once("http.php");
require_once("utils.php");
require_once("digest.php");
//------------------------------普通上传------------------------------
function UCloud_PutFile($bucket, $key, $file)
{
$action_type = ActionType::PUTFILE;
$err = CheckConfig(ActionType::PUTFILE);
if ($err != null) {
return array(null, $err);
}
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = $key;
$content = @fread($f, filesize($file));
list($mimetype, $err) = GetFileMimeType($file);
if ($err) {
fclose($f);
return array("", $err);
}
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
$req->Header['Expect'] = '';
$req->Header['Content-Type'] = $mimetype;
$client = new UCloud_AuthHttpClient(null, $mimetype);
list($data, $err) = UCloud_Client_Call($client, $req);
fclose($f);
return array($data, $err);
}
//------------------------------表单上传------------------------------
function UCloud_MultipartForm($bucket, $key, $file)
{
$action_type = ActionType::POSTFILE;
$err = CheckConfig(ActionType::POSTFILE);
if ($err != null) {
return array(null, $err);
}
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = "";
$fsize = filesize($file);
$content = "";
if ($fsize != 0) {
$content = @fread($f, filesize($file));
if ($content == FALSE) {
fclose($f);
return array(null, new UCloud_Error(0, -1, "read file error"));
}
}
list($mimetype, $err) = GetFileMimeType($file);
if ($err) {
fclose($f);
return array("", $err);
}
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
$req->Header['Expect'] = '';
$token = UCloud_SignRequest(null, $req, $mimetype);
$fields = array('Authorization'=>$token, 'FileName' => $key);
$files = array('files'=>array('file', $file, $content, $mimetype));
$client = new UCloud_AuthHttpClient(null, NO_AUTH_CHECK);
list($data, $err) = UCloud_Client_CallWithMultipartForm($client, $req, $fields, $files);
fclose($f);
return array($data, $err);
}
//------------------------------分片上传------------------------------
function UCloud_MInit($bucket, $key)
{
$err = CheckConfig(ActionType::MINIT);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = $key;
$querys = array(
"uploads" => ""
);
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call($client, $req);
}
//@results: (tagList, err)
function UCloud_MUpload($bucket, $key, $file, $uploadId, $blkSize, $partNumber=0)
{
$err = CheckConfig(ActionType::MUPLOAD);
if ($err != null) {
return array(null, $err);
}
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$etagList = array();
list($mimetype, $err) = GetFileMimeType($file);
if ($err) {
fclose($f);
return array("", $err);
}
$client = new UCloud_AuthHttpClient(null);
for(;;) {
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = $key;
if (@fseek($f, $blkSize*$partNumber, SEEK_SET) < 0) {
fclose($f);
return array(null, new UCloud_Error(0, -1, "fseek error"));
}
$content = @fread($f, $blkSize);
if ($content == FALSE) {
if (feof($f)) break;
fclose($f);
return array(null, new UCloud_Error(0, -1, "read file error"));
}
$querys = array(
"uploadId" => $uploadId,
"partNumber" => $partNumber
);
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path, 'query'=>$querys), $content, $bucket, $key);
$req->Header['Content-Type'] = $mimetype;
$req->Header['Expect'] = '';
list($data, $err) = UCloud_Client_Call($client, $req);
if ($err) {
fclose($f);
return array(null, $err);
}
$etag = @$data['ETag'];
$part = @$data['PartNumber'];
if ($part != $partNumber) {
fclose($f);
return array(null, new UCloud_Error(0, -1, "unmatch partnumber"));
}
$etagList[] = $etag;
$partNumber += 1;
}
fclose($f);
return array($etagList, null);
}
function UCloud_MFinish($bucket, $key, $uploadId, $etagList, $newKey = '')
{
$err = CheckConfig(ActionType::MFINISH);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = $key;
$querys = array(
'uploadId' => $uploadId,
'newKey' => $newKey,
);
$body = @implode(',', $etagList);
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), $body, $bucket, $key);
$req->Header['Content-Type'] = 'text/plain';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call($client, $req);
}
function UCloud_MCancel($bucket, $key, $uploadId)
{
$err = CheckConfig(ActionType::MCANCEL);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = $key;
$querys = array(
'uploadId' => $uploadId
);
$req = new HTTP_Request('DELETE', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call($client, $req);
}
//------------------------------秒传------------------------------
function UCloud_UploadHit($bucket, $key, $file)
{
$err = CheckConfig(ActionType::UPLOADHIT);
if ($err != null) {
return array(null, $err);
}
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
$content = "";
$fileSize = filesize($file);
if ($fileSize != 0) {
$content = @fread($f, $fileSize);
if ($content == FALSE) {
fclose($f);
return array(null, new UCloud_Error(0, -1, "read file error"));
}
}
list($fileHash, $err) = UCloud_FileHash($file);
if ($err) {
fclose($f);
return array(null, $err);
}
fclose($f);
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = "uploadhit";
$querys = array(
'Hash' => $fileHash,
'FileName' => $key,
'FileSize' => $fileSize
);
$req = new HTTP_Request('POST', array('host'=>$host, 'path'=>$path, 'query'=>$querys), null, $bucket, $key);
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call($client, $req);
}
//------------------------------删除文件------------------------------
function UCloud_Delete($bucket, $key)
{
$err = CheckConfig(ActionType::DELETE);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = "$key";
$req = new HTTP_Request('DELETE', array('host'=>$host, 'path'=>$path), null, $bucket, $key);
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call($client, $req);
}
//------------------------------Head文件------------------------------
function UCloud_Head($bucket, $key)
{
$err = CheckConfig(ActionType::HEAD);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$path = "$key";
$req = new HTTP_Request('HEAD', array('host'=>$host, 'path'=>$path), null, $bucket, $key);
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
$client = new UCloud_AuthHttpClient(null);
return UCloud_Client_Call_ReHeader($client, $req);
}
//------------------------------追加上传------------------------------
function UCloud_AppendFile($bucket, $key, $file, $position)
{
$action_type = ActionType::APPENDFILE;
$err = CheckConfig(ActionType::APPENDFILE);
if ($err != null) {
return array(null, $err);
}
$f = @fopen($file, "r");
if (!$f) return array(null, new UCloud_Error(-1, -1, "open $file error"));
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$key = $key . "?append&position=" . $position;
$path = $key;
$content = @fread($f, filesize($file));
list($mimetype, $err) = GetFileMimeType($file);
if ($err) {
fclose($f);
return array("", $err);
}
$req = new HTTP_Request('PUT', array('host'=>$host, 'path'=>$path), $content, $bucket, $key, $action_type);
$req->Header['Expect'] = '';
$req->Header['Content-Type'] = $mimetype;
$client = new UCloud_AuthHttpClient(null, $mimetype);
list($data, $err) = UCloud_Client_Call($client, $req);
fclose($f);
return array($data, $err);
}
//------------------------------列表目录------------------------------
function UCloud_ListObjects($bucket, $path_prefix, $marker, $count, $delimiter)
{
$action_type = ActionType::LISTOBJECTS;
$err = CheckConfig(ActionType::LISTOBJECTS);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PROXY_SUFFIX;
$host = $bucket . $UCLOUD_PROXY_SUFFIX;
$query = "listobjects&prefix=" . $path_prefix ."&marker=". $marker . "&max-keys=" . $count ."&delimiter=" .$delimiter;
parse_str($query,$arr);
$path = "?" . http_build_query($arr);
$req = new HTTP_Request('GET', array('host'=>$host, 'path'=>$path), null, $bucket, null, $action_type);
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
$client = new UCloud_AuthHttpClient(null);
list($data, $err) = UCloud_Client_Call($client, $req);
return array($data, $err);
}
//------------------------------生成公有文件Url------------------------------
// @results: $url
function UCloud_MakePublicUrl($bucket, $key)
{
global $UCLOUD_PROXY_SUFFIX;
return $bucket . $UCLOUD_PROXY_SUFFIX . "/" . rawurlencode($key);
}
//------------------------------生成私有文件Url------------------------------
// @results: $url
function UCloud_MakePrivateUrl($bucket, $key, $expires = 0)
{
$err = CheckConfig(ActionType::GETFILE);
if ($err != null) {
return array(null, $err);
}
global $UCLOUD_PUBLIC_KEY;
$public_url = UCloud_MakePublicUrl($bucket, $key);
$req = new HTTP_Request('GET', array('path'=>$public_url), null, $bucket, $key);
if ($expires > 0) {
$req->Header['Expires'] = $expires;
}
$client = new UCloud_AuthHttpClient(null);
$temp = $client->Auth->SignRequest($req, null, QUERY_STRING_CHECK);
$signature = substr($temp, -28, 28);
$url = $public_url . "?UCloudPublicKey=" . rawurlencode($UCLOUD_PUBLIC_KEY) . "&Signature=" . rawurlencode($signature);
if ('' != $expires) {
$url .= "&Expires=" . rawurlencode($expires);
}
return $url;
}

View File

@ -17,6 +17,8 @@ abstract class ActionType
const UPLOADHIT = 7;
const GETFILE = 8;
const APPENDFILE = 9;
const LISTOBJECTS = 10;
const HEAD = 11;
}
class UCloud_Error
@ -125,6 +127,8 @@ function CheckConfig($action) {
case ActionType::MFINISH:
case ActionType::DELETE:
case ActionType::UPLOADHIT:
case ActionType::LISTOBJECTS:
case ActionType::HEAD:
if ($UCLOUD_PROXY_SUFFIX == "") {
return new UCloud_Error(400, -1, "no proxy suffix found in config");
} else if ($UCLOUD_PUBLIC_KEY == "" || strstr($UCLOUD_PUBLIC_KEY, " ") != FALSE) {